Understanding Linux Memory Cache: Buffers, Cache, Metrics, and Monitoring Tools
This guide explains Linux in‑memory caching, distinguishes buffers from cache, shows how to interpret cache metrics like hit rate, provides commands to clear caches, and introduces the cachestat and cachetop tools for detailed cache analysis.
1. In‑memory cache
Linux memory cache consists of buffers and cache, which can be observed with the free command.
# 注意不同版本的free输出可能会有所不同
$ free
total used free shared buff/cache available
Mem: 8169348 263524 6875352 668 1030472 7611064
Swap: 0 0 0Buffer caching is performed directly on raw disk block devices by programs, while cache data passes through the filesystem (ext, xfs, etc.) before being cached.
buffer: program uses raw disk block device, caching data.
cache: data goes through the filesystem and is then cached.
2. Cache metrics
Cache utilization is measured by the hit‑rate metric; a higher hit rate indicates more effective cache use and better application performance. Buffers and cache dramatically improve disk I/O by bridging slow disk access with fast memory. To drop caches, run:
# 清理缓存
$ echo 3 > /proc/sys/vm/drop_caches3. Tools for analyzing cache
cachestat monitors Linux page‑cache hit rates. The command cachestat 1 3 samples every second for three intervals. Example output and field meanings are:
root@test:~# cachestat 1 3
HITS MISSES DIRTIES HITRATIO BUFFERS_MB CACHED_MB
14076 0 0 100.00% 1084 484807
0 0 0 0.00% 1084 484807
0 0 0 0.00% 1084 484807HITS – number of cache hits.
MISSES – number of cache misses.
DIRTIES – number of dirty pages added to cache.
HITRATIO – cache hit rate.
BUFFERS_MB – size of Buffers in megabytes.
CACHED_MB – size of Cache in megabytes.
cachetop displays per‑process cache statistics, including hits, misses, dirties, and read/write hit percentages.
17:56:37 Buffers MB: 1084 / Cached MB: 484808 / Sort: HITS / Order: ascending
PID UID CMD HITS MISSES DIRTIES READ_HIT% WRITE_HIT%
12210 root pg_watcher_serv 15 0 0 100.0% 0.0%
14835 root cachetop 31 0 0 100.0% 0.0%
14880 root ping 359 0 0 100.0% 0.0%
14879 root ping 376 0 0 100.0% 0.0%
14878 root ping 418 0 0 100.0% 0.0%
14881 root nc 481 0 0 100.0% 0.0%Both tools require the bcc-tools package:
sudo apt install -y bcc-toolsTech Stroll Journey
The philosophy behind "Stroll": continuous learning, curiosity‑driven, and practice‑focused.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
