buffer 与cache :
通过 $ man free
Buffers Memory used by kernel buffers (Buffers in /proc/meminfo)
cache Memory used by the page cache and slabs (Cached and SReclaimable in /proc/meminfo)
buff/cache Sum of buffers and cache
可以看到,buffers是内核缓冲区用到的内存;cache是内核页缓存和Slab用到的内存,cache大小等于Cached +SReclaimable。
$man proc
Buffers %lu
Relatively temporary storage for raw disk blocks that shouldn't get tremendously large (20MB or so).
Cached %lu
In-memory cache for files read from the disk (the page cache). Doesn't include SwapCached.
...
SReclaimable %lu (since Linux 2.6.19)
Part of Slab, that might be reclaimed, such as caches.
SUnreclaim %lu (since Linux 2.6.19)
Part of Slab, that cannot be reclaimed on memory pressure.
上面这段可以简单理解为:
CPU(写) ------> buffer ------> disk(解决CPU与硬盘性能差异,待写数据放入buffer池)
CPU(读)<------ cache <------ disk(命中缓存,不用每次去disk获取数据,命中率越高,IO读(bi)越小)
A buffer is something that has yet to be "written" to disk.
A cache is something that has been "read" from the disk and stored for later use.
$ free -m
total used free shared buffers cached
Mem: 2006 1735 270 1 39 728
-/+ buffers/cache: 967 1038
Swap: 0 0 0
# total = used + free + shared
'-buffers/cache used' = 第一行used - buffers - cached - shared;
'+buffers/cache free' = 第一行free + buffers + cached + shared;
#内存不足,会触发缓存回收,swap机制和OOM。Swap 越高,说明内核置换cache越频繁,内存资源越紧张。
$ vmstat 1 3 #查看bi,bi:磁盘读,bo:磁盘写
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 0 277136 40616 745644 0 0 88 237 1188 526 7 2 91 0 0
0 0 0 277136 40632 745636 0 0 0 64 1264 628 0 0 100 0 0
0 0 0 277136 40632 745644 0 0 0 4 1258 607 0 0 100 0 0
#cache越大,bi越小