In Linux, what is the difference between “buffers” and “cache” reported by the free command?
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.
Freeing buffer/cache
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.
buffer & cache
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.
解释参考:Difference Between Buffer and Cache, orHere
对于共享内存(Shared memory),主要用于在UNIX 环境下不同进程之间共享数据,是进程间通信的一种方法,一般的应用程序不会申请使用共享内存,笔者也没有去验证共享内存对上面等式的影响。如果你有兴趣,请参考:What is Shared Memory?
cache 和 buffer的区别:
Cache:高速缓存,是位于CPU与主内存间的一种容量较小但速度很高的存储器。由于CPU的速度远高于主内存,CPU直接从内存中存取数据要等待一定时间周期, Cache中保存着CPU刚用过或循环使用的一部分数据,当CPU再次使用该部分数据时可从Cache中直接调用,这样就减少了CPU的等待时间,提高了系统的效率。Cache又分为一级Cache(L1 Cache)和二级Cache(L2 Cache),L1 Cache集成在CPU内部,L2 Cache早期一般是焊在主板上,现在也都集成在CPU内部,常见的容量有256KB或512KB L2 Cache。
Buffer:缓冲区,一个用于存储速度不同步的设备或优先级不同的设备之间传输数据的区域。通过缓冲区,可以使进程之间的相互等待变少,从而使从速度慢的设备读入数据时,速度快的设备的操作进程不发生间断。
Free中的buffer和cache:(它们都是占用内存):
buffer : 作为buffer cache的内存,是块设备的读写缓冲区
cache: 作为page cache的内存, 文件系统的cache
如果 cache 的值很大,说明cache住的文件数很多。如果频繁访问到的文件都能被cache住,那么磁盘的读IO bi会非常小。
一句话区分:readcache,writebuffer. cache常用于表示读缓存:memcache; buffer常用于写缓存: Intel CPU store buffer; 当然, 更常见的是二者混用: Oracle/MySQL buffer cache!(@here)
page cache和buffer cache最大的差别在于:page cache是对文件数据的缓存;buffer cache是对设备数据的缓存!