User Agents

Web browsers and everyday web tech, explained

See Cache Statistics in Linux (Commands)

· 5 min read · Cookies and Cache

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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 sar historical data: the sysstat package installed (e.g., sudo apt install sysstat on Debian/Ubuntu, sudo yum install sysstat on RHEL/CentOS)

Step-by-Step

  1. Open a terminal – press Ctrl+Alt+T (or use your desktop's terminal app).
  2. Run free -h – this shows memory in human-readable format. Look at the buff/cache column; 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
    The buff/cache value includes both page cache and buffer cache.
  3. Check /proc/meminfo – for detailed breakdown, run cat /proc/meminfo. Look for lines like Cached, Buffers, SReclaimable (slab reclaimable). Example:
    Cached:          3456780 kB
    Buffers:          123456 kB
    SReclaimable:     456789 kB
    Cached is the page cache (file data), Buffers is raw disk block cache, and SReclaimable is kernel slab memory that can be reclaimed.
  4. Use vmstat – run vmstat 1 5 to sample every second for 5 seconds. The cache column (under memory) shows cache size in KB. Also watch si and so (swap in/out) – high values might indicate cache pressure.
  5. Use sar for historical stats – if sysstat is installed, run sar -r 1 3 to show memory statistics every second for 3 intervals. Look for kbcached (cache size in KB) and kbcommit. To see daily history, run sar -r -f /var/log/sysstat/sa$(date +%d) (adjust path if different).
  6. Check cache hit ratio (optional) – for filesystem cache, you can use cachestat from the perf-tools package (if installed). Run sudo cachestat to see hits/misses per second. If not installed, you can approximate by reading /proc/vmstat for pgpgin and pgpgout – but exact hit ratio is not directly available without tools like cachestat.
  7. For page cache specifically – run grep -E '^(Cached|Buffers|SReclaimable)' /proc/meminfo to get a clean output.

Common Mistakes

  • Misinterpreting free output – The free command's buff/cache is not “wasted” memory; it's used for caching and can be freed when needed. Don't assume high cache means low available memory.
  • Using free without -h – Output in bytes is hard to read; always use -h for human-readable.
  • Expecting vmstat to show cache in MB – The cache column is in KB, not MB. Divide by 1024 to get MB.
  • Forgetting that sar needs root – Historical data files are often only readable by root. Use sudo sar if 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 cachestat are needed and may not be pre-installed.

Tips

  • Use watch to monitor continuously – Run watch -n 2 free -h to update every 2 seconds.
  • Combine with iostat – To see if cache is effective, run iostat -x 1 alongside vmstat to 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. Run cat /proc/pressure/memory.
  • For detailed slab info – Use cat /proc/slabinfo (requires root) to see what kernel objects are cached.

Troubleshooting

  • If sar is not found – Install sysstat package. On Debian/Ubuntu: sudo apt install sysstat. On RHEL/CentOS: sudo yum install sysstat. After installation, restart the sysstat service: sudo systemctl restart sysstat (or sudo service sysstat restart).
  • If vmstat shows 0 cache – Check your kernel version; very old kernels might not report cache in vmstat. Use free instead.
  • If cachestat is not available – Install perf-tools from your distro's repo (e.g., sudo apt install perf-tools on Ubuntu). If not available, you can use perf with tracepoints, but that's advanced.
  • If /proc/meminfo shows 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 – Use sudo to 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.