See Cache Statistics in Linux (Commands)
In this article
To see cache statistics in Linux, you use commands like free, vmstat, /proc/meminfo, and sar (if installed). These show how much memory is used for caching (page cache, dentries, inodes) and how effectively the cache is being used. No special privileges are needed for most commands, but some (like sar) may require the sysstat package and root access to read historical data.
What You'll Need
- A Linux terminal (any distribution)
- Basic familiarity with command line navigation
- For
sarhistorical data: thesysstatpackage installed (e.g.,sudo apt install sysstaton Debian/Ubuntu,sudo yum install sysstaton RHEL/CentOS)
Step-by-Step
- Open a terminal – press Ctrl+Alt+T (or use your desktop's terminal app).
- Run
free -h– this shows memory in human-readable format. Look at thebuff/cachecolumn; that's your cache usage. Example output:total used free shared buff/cache available Mem: 15Gi 2.1Gi 8.5Gi 245Mi 4.4Gi 12Gi Swap: 2.0Gi 0B 2.0Gi
Thebuff/cachevalue includes both page cache and buffer cache. - Check
/proc/meminfo– for detailed breakdown, runcat /proc/meminfo. Look for lines likeCached,Buffers,SReclaimable(slab reclaimable). Example:Cached: 3456780 kB Buffers: 123456 kB SReclaimable: 456789 kB
Cachedis the page cache (file data),Buffersis raw disk block cache, andSReclaimableis kernel slab memory that can be reclaimed. - Use
vmstat– runvmstat 1 5to sample every second for 5 seconds. Thecachecolumn (under memory) shows cache size in KB. Also watchsiandso(swap in/out) – high values might indicate cache pressure. - Use
sarfor historical stats – ifsysstatis installed, runsar -r 1 3to show memory statistics every second for 3 intervals. Look forkbcached(cache size in KB) andkbcommit. To see daily history, runsar -r -f /var/log/sysstat/sa$(date +%d)(adjust path if different). - Check cache hit ratio (optional) – for filesystem cache, you can use
cachestatfrom theperf-toolspackage (if installed). Runsudo cachestatto see hits/misses per second. If not installed, you can approximate by reading/proc/vmstatforpgpginandpgpgout– but exact hit ratio is not directly available without tools likecachestat. - For page cache specifically – run
grep -E '^(Cached|Buffers|SReclaimable)' /proc/meminfoto get a clean output.
Common Mistakes
- Misinterpreting
freeoutput – Thefreecommand'sbuff/cacheis not “wasted” memory; it's used for caching and can be freed when needed. Don't assume high cache means low available memory. - Using
freewithout-h– Output in bytes is hard to read; always use-hfor human-readable. - Expecting
vmstatto show cache in MB – Thecachecolumn is in KB, not MB. Divide by 1024 to get MB. - Forgetting that
sarneeds root – Historical data files are often only readable by root. Usesudo sarif you get permission errors. - Assuming cache hit ratio is easy to get – Linux doesn't expose a simple hit ratio for page cache; tools like
cachestatare needed and may not be pre-installed.
Tips
- Use
watchto monitor continuously – Runwatch -n 2 free -hto update every 2 seconds. - Combine with
iostat– To see if cache is effective, runiostat -x 1alongsidevmstatto correlate disk I/O with cache size. - Understand that cache is good – High cache usage usually means the system is using memory efficiently. Only worry if available memory is consistently low.
- Check
/proc/pressure/memory– This shows memory pressure (stall times) which can indicate if cache is causing issues. Runcat /proc/pressure/memory. - For detailed slab info – Use
cat /proc/slabinfo(requires root) to see what kernel objects are cached.
Troubleshooting
- If
saris not found – Installsysstatpackage. On Debian/Ubuntu:sudo apt install sysstat. On RHEL/CentOS:sudo yum install sysstat. After installation, restart the sysstat service:sudo systemctl restart sysstat(orsudo service sysstat restart). - If
vmstatshows 0 cache – Check your kernel version; very old kernels might not report cache in vmstat. Usefreeinstead. - If
cachestatis not available – Installperf-toolsfrom your distro's repo (e.g.,sudo apt install perf-toolson Ubuntu). If not available, you can useperfwith tracepoints, but that's advanced. - If
/proc/meminfoshows huge values – That's normal on systems with lots of RAM. Don't panic; cache is reclaimable. - If you get permission denied on
/proc/slabinfo– Usesudoto read it.
Frequently Asked Questions
What is the difference between buff/cache and available memory?
buff/cache is memory used for caching, which can be freed when needed. available is an estimate of memory available for starting new applications without swapping. High cache is fine if available is reasonable.
How do I clear the cache?
You can drop caches with sudo sync; echo 3 > /proc/sys/vm/drop_caches. This is not recommended for production because it degrades performance temporarily. It's mainly for benchmarking.
Why is my cache so large?
Linux aggressively uses free memory for caching to speed up file I/O. It's normal to see 70-80% of RAM used for cache on a busy system. The kernel will reclaim it when applications need memory.
Can I see cache statistics per process?
Not directly. Page cache is global, not per-process. However, you can use smem (if installed) to estimate per-process memory including shared pages, but it's not exact.
What does pgpgin in /proc/vmstat mean?
It's the total number of kilobytes loaded from disk to memory (page-in). High values indicate cache misses. You can monitor this with watch grep pgpgin /proc/vmstat.
Is there a graphical tool to see cache stats?
Yes, tools like htop (press F2 to enable memory bars) or glances show cache usage visually. Install them with your package manager.