Understanding “Buffers” and “Cached” from free com

2019-03-08 06:36发布

问题:

This has been asked earlier but don't want to update the same thread again as it was a old thread .

Want to clarify myself on the "buffers" and "cache" column from the output of free command.

This is what my understanding...

Buffer is something where data is there in memory but yet to be flushed to disk . The data will be flushed to disk by bdflush daemon periodically or we can do it manually by running sync command .

Cache on the other hand is program/data which is loaded into memory but is retained in memory so that if is needed again , it will be quickly available.

To understand the concept of buffers , I tried the following experiment...

This is the reading of free command in my desktop

[zama@localhost ~]$ free -m
             total       used       free     shared    buffers     cached
Mem:          2897        465       2431          0         30        230
-/+ buffers/cache:        204       2692
Swap:         4000          0       4000

[zama@localhost ~]$ sync

[zama@localhost ~]$ free -m
             total       used       free     shared    buffers     cached
Mem:          2897        466       2431          0         30        230
-/+ buffers/cache:        205       2691
Swap:         4000          0       4000

Here I cannot see buffer getting reduced after executing the sync command.

Next I tried the following...Tried to write a huge file to the disk .

[zama@localhost ~]$ dd if=/dev/zero of=test bs=1024k 

As expected , the cached value should increase and free is confirming this..

@localhost ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          2897       1466       1430          0         32       1127
-/+ buffers/cache:        306       2590
Swap:         4000          0       4000

I again executed the sync command and then checked using free . I can see that the buffer value getting decreased from the output of free command . There was no reduction in the cache . This means that the dirty pages in RAM after my execution of dd coomand has been flushed to disk .

@localhost ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          2897       1466       1430          0         10       1127
-/+ buffers/cache:        306       2590
Swap:         4000          0       4000

Then I updated the drop_cache kernel parameter so that the cache vlaue is dropped

[root@localhost ~]# cat /proc/sys/vm/drop_caches 
0

[root@localhost ~]# echo "1" > /proc/sys/vm/drop_caches 

[root@localhost ~]# cat /proc/sys/vm/drop_caches 
1

free now confirms that both buffer and cache value is dropped.

[root@localhost ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          2897        299       2597          0          1         74
-/+ buffers/cache:        224       2672
Swap:         4000          0       4000

So , my initial statement that "Buffer" is RAM data which is yet to be flushed to disk looks to be correct .

Please guide me whether I am in the right direction.

回答1:

The column headers in the free command are somewhat mislabeled, at least from the point of view of a linux user (as opposed to developer). Below is a clarification of what the headings mean:

total: Yes, this is total ram.

used: This is probably the most confused column. This is a mix of application used memory and other 'temporarily' (buffer + cache) used memory that is actually available if needed. So technically the memory is truly being used, but much of this memory is available if an application needs it. The 'temporarily' used memory is borrowed if available by the linux system to help speed up system performance, otherwise the system would have read from disk more often. Much of this type of memory is shown under the 'cached' column. This memory is given up by the linux system if an application need memory.

free: Yes, this pure free and untouched memory.

shared: Memory specifically allocated for use by multiple processes

buffers: Temporary memory that is set aside to help some processes

cache: Memory that is available and 'borrowed' by the operating system to help speed up many linux OS operations. This memory is given up by the system if an application need it.

The line that starts with -/+ buffers/cache is typically more helpful than the first Mem line. The intersection of free and -/+ buffers/cache is essentially what you have for 'available' memory.



回答2:

The way I've always understood it is that the buffer area of memory is for temporary storage of data being read from or written to devices (including disks), while the cache area of memory is for speeding up future reads from a device.



回答3:

Linux vm sub-system treats any free or unused or non-allocated memory as buffers/cache

Therefore, running echo 1 > /proc/sys/vm/drop_caches instructs the kernel to drop or clean the page caches (page-cache), dentries (slab-cache), and inodes (in the slab-cache) and causing the memory to become reclaimed and available.

Clean data cache pages are not freed by design. They can be easily reclaimed by the kernel if or when extra memory is to be allocated (malloc/calloc/brk/sbrk and friends), while they contain useful data from disks that if needed again saves doing a disk I/O.

sync command only commits any dirty pages in the memory to the disk, it does not free the buffers/cache. The only way to drop buffers/cache is by using echo "1" > /proc/sys/vm/drop_caches or the kernel reclaims the data pages for new allocated requested by applications

pdflush has been replaced by using flushing threads per backing device info (BDI).



回答4:

Quoting from RHEL:

Linux always tries to use RAM to speed up disk operations by using available memory for buffers (file system metadata) and cache (pages with actual contents of files or block devices).



回答5:

This is my understanding: the buffers tells you how much memory is reserved for maintaining block devices, but cache tells you how many memory is used for file contents.