~~
This is an old question that I've seen from time to time. My understanding of it is rather limited (having read about the differences a long time ago, but the factoid(s) involved never really stuck).
As I understand it,
Buffers
Are used by programs with active I/O operations, i.e. data waiting to be written to disk
Cache
Is the result of completed I/O operations, i.e. buffers that have been flushed or data read from disk to satisfy a request.
The "cached" total will also include some other memory allocations, such as any tmpfs filesytems. To see this in effect try:
mount -ttmpfs none t
dd if=dev/zero of=t/zero.file bs=10240 count=10240
sync; echo 3 > /proc/sys/vm/drop_caches; free -m
umount t
sync; echo 3 > /proc/sys/vm/drop_caches; free -m
and you will see the "cache" value drop by the 100Mb that you copied to the ram-based filesystem (assuming there was enough free RAM, you might find some of it ended up in swap if the machine is already over-committed in terms of memory use). The "sync; echo 3 > /proc/sys/vm/drop_caches" before each call to free should write anything pending in all write buffers (the sync) and clear all cached/buffered disk blocks from memory so free will only be reading other allocations in the "cached" value.
The RAM used by virtual machines (such as those running under VMWare) will also be counted in free's "cached" value, as will RAM used by currently open memory-mapped files.
So it isn't as simple as "buffers counts pending file/network writes and cached counts recently read/written blocks held in RAM to save future physical reads", though for most purposes this simpler description will do.
Warning This explain a strong method not recommended on production server! So you're warned, don't blame me if something goes wrong.
For understanding, the thing, you could force your system to delegate as many memory as possible tocache
than drop the cached file:
Before of doing the test, you could open another window an hit:
$ vmstat -n 1
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
0 1 39132 59740 39892 1038820 0 0 1 0 3 3 5 13 81 1
1 0 39132 59140 40076 1038812 0 0 184 0 10566 2157 27 15 48 11
...
for following evolution of swap in real time.
Nota: You must dispose of as many disk free on current directory, you have mem+swap
The demo$ free
total used free shared buffers cached
Mem: 2064396 2004320 60076 0 90740 945964
-/+ buffers/cache: 967616 1096780
Swap: 3145720 38812 3106908
$ tot=0
$ while read -a line;do
[[ "${line%:}" =~ ^(Swap|Mem)Total$ ]] && ((tot+=2*${line[1]}))
done </proc/meminfo
$ echo $tot
10420232
$ dd if=/dev/zero of=veryBigFile count=$tot
10420232+0 records in
10420232+0 records out
5335158784 bytes (5.3 GB) copied, 109.526 s, 48.7 MB/s
$ cat >/dev/null veryBigFile
$ free
total used free shared buffers cached
Mem: 2064396 2010160 54236 0 41568 1039636
-/+ buffers/cache: 928956 1135440
Swap: 3145720 39132 3106588
$ rm veryBigFile
$ free
total used free shared buffers cached
Mem: 2064396 1005104 1059292 0 41840 48124
-/+ buffers/cache: 915140 1149256
Swap: 3145720 39132 3106588
Nota, the host on wich I've done this is strongly used. This will be more significant on a really quiet machine.
如果 cache 的值很大,说明cache住的文件数很多。如果频繁访问到的文件都能被cache住,那么磁盘的读IO bi会非常小。
一句话区分:read cache,write buffer. cache常用于表示读缓存:memcache; buffer常用于写缓存: Intel CPU store buffer; 当然, 更常见的是二者混用: Oracle/MySQL buffer cache!(@here)
参考1
page cache和buffer cache最大的差别在于:page cache是对文件数据的缓存;buffer cache是对设备数据的缓存!
参考2