Chinaunix首页 | 论坛 | 博客
  • 博客访问: 773558
  • 博文数量: 196
  • 博客积分: 115
  • 博客等级: 民兵
  • 技术积分: 354
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-13 23:19
文章分类

全部博文(196)

文章存档

2021年(1)

2019年(5)

2018年(11)

2017年(15)

2016年(13)

2015年(46)

2014年(81)

2013年(22)

2012年(2)

分类: LINUX

2013-09-23 17:30:52

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

阅读(1359) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~