LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-09-2012, 07:45 AM   #1
zama
Member
 
Registered: Mar 2012
Posts: 34

Rep: Reputation: Disabled
Understanding “Buffers” and “Cached” from free command


Hi All,

The query is 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 command 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 value 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

I can see that cache value is dropped , but also buffer value is also dropped. free confirms this..

[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 ..

Thanks in Advance
 
Old 04-09-2012, 07:57 AM   #2
ericson007
Member
 
Registered: Sep 2004
Location: Japan
Distribution: CentOS 7.1
Posts: 735

Rep: Reputation: 154Reputation: 154
I never really thought of buffers vs cache before, but you seem to have a solid case in your way of reasoning. I would agree to your statements.

Quoted from the #man free

Quote:
The second line of data, which begins with -/+ buffers/cache, shows the amount of physical memory currently devoted to system buffer cache. This is particularly meaningful with regard to application programs, as all data accessed from files on the system that are performed through the use of read() and write() system calls1 pass through this cache. This cache can greatly speed up access to data by reducing or eliminating the need to read from or write to the HDD or other disk.
 
Old 04-09-2012, 08:17 AM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by zama View Post
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.
I'm pretty sure your distinction between buffers and cache is completely wrong.

I don't fully understand the distinction myself, but I think the underlying issue behind the distinction is page alignment.

Caching in a paged OS is simplest when an entire page (normally 4KB) is aligned on a page boundary in the file itself, in physical memory of the cache and in the virtual address mapping that uses it (user address space if it is a mapped page or kernel address space for other forms of file I/O).

I believe cache is used for caching things that can be handled as full aligned pages and buffers are used for caching things that can't.

That should be orthogonal to the distinction between data that has been modified and is awaiting write back and data that is kept in case it will be read again.

Last edited by johnsfine; 04-09-2012 at 08:19 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
/proc/meminfo interpret values of Buffers Cached blockdump Linux - General 4 03-30-2012 05:32 AM
Linux memory management: Real memory or Cached buffers? gubbu Linux - Server 2 10-01-2010 01:58 AM
[SOLVED] Existeth a way to 'clear out' all the old/cached crap from RAM/SWAP to free up space? GrapefruiTgirl Linux - General 37 07-29-2010 12:46 PM
skb buffers free while working with PF_PACKET socket igalch Linux - Kernel 2 04-06-2010 09:45 AM
buffers and cached groing up to total!? tovis Programming 2 01-26-2007 06:24 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration