memorylinux-kernelfree-command

Understanding "Buffers" and "Cached" from free command


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.


Solution

  • 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: Slightly confusing. This is calculated as total - (free + buffers + cache)

    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.