吐血推荐文章: Linux内存中的Cache真的能被回收么?

free中的buffer和cache:

redhat对free输出的解读

两者都是RAM中的数据。简单来说,buffer是即将要被写入磁盘的,而cache是被从磁盘中读出来的。 (free中的buffer和cach它们都是占用内存的)

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

buffer

buffer : 作为buffer cache的内存,是块设备的写缓冲区。buffer是根据磁盘的读写设计的,把分散的写操作集中进行,减少磁盘碎片和硬盘的反复寻道,从而提高系统性能。linux有一个守护进程定期清空缓冲内容(即写如磁盘),也可以通过sync命令手动清空缓冲。buffer是由各种进程分配的,被用在如输入队列等方面,一个简单的例子如某个进程要求有多个字段读入,在所有字段被读入完整之前,进程把先前读入的字段放在buffer中保存。

cache

cache: 作为page cache的内存, 文件系统的cache。cache经常被用在磁盘的I/O请求上,如果有多个进程都要访问某个文件,于是该文件便被做成cache以方便下次被访问,这样可提供系统性能。cache是把读取过的数据保存起来,重新读取时若命中(找到需要的数据)就不要去读硬盘了,若没有命中就读硬盘。其中的数据会根据读取频率进行组织,把最频繁读取的内容放在最容易找到的位置,把不再读的内容不断往后排,直至从中删除。  如果 cache 的值很大,说明cache住的文件数很多。如果频繁访问到的文件都能被cache住,那么磁盘的读IO bi会非常小。

el6:

  1. free 命令,在el6 el7上的输出是不一样的;
  2. 对于el6 ,看真正的有多少内存是free的,应该看 free的第二行!!!
[root@Linux ~]# free
total used free shared buffers cached
Mem: 8054344 1834624 6219720 0 60528 369948
-/+ buffers/cache: 1404148 6650196
Swap: 524280 144 524136

第1行:

total 内存总数: 8054344

used 已经使用的内存数: 1834624

free 空闲的内存数: 6219720

shared 当前已经废弃不用,总是0

buffers Buffer Cache内存数: 60528 (缓存文件描述信息)

cached Page Cache内存数: 369948 (缓存文件内容信息)

关系:total = used + free

第2行:

-/+ buffers/cache的意思相当于:

-buffers/cache 的内存数:1404148 (等于第1行的 used - buffers - cached)

+buffers/cache 的内存数:6650196 (等于第1行的 free + buffers + cached)

可见-buffers/cache反映的是被程序实实在在吃掉的内存,而+buffers/cache反映的是可以使用的内存总数。

释放掉被系统cache占用的数据:

  1. 手动执行sync命令(描述:sync 命令运行 sync 子例程。如果必须停止系统,则运行sync 命令以确保文件系统的完整性。sync 命令将所有未写的系统缓冲区写到磁盘中,包含已修改的 i-node、已延迟的块 I/O 和读写映射文件)

有关/proc/sys/vm/drop_caches的用法在下面进行了说明:

/proc/sys/vm/drop_caches (since Linux 2.6.16)

Writing to this file causes the kernel to drop clean caches,dentries and inodes from memory, causing that memory to become free.

To free pagecache, use echo 1 > /proc/sys/vm/drop_caches;

to free dentries and inodes, use echo 2 > /proc/sys/vm/drop_caches;

to free pagecache, dentries and inodes, use echo 3 > /proc/sys/vm/drop_caches.

Because this is a non-destructive operation and dirty objects are not freeable, the user should run sync first.

echo 3>/proc/sys/vm/drop_caches

el7

[root@jiangyi01.sqa.zmf /home/ahao.mah]
#free -lm
total used free shared buff/cache available
Mem: 96479 6329 84368 201 5781 89491
Low: 96479 12110 84368
High: 0 0 0
Swap: 2047 2047 0
[root@jiangyi01.sqa.zmf /home/ahao.mah]
#free -k;cat /proc/meminfo | grep MemAvailable
total used free shared buff/cache available
Mem: 98795000 6481796 86390012 206496 5923192 91638016
Swap: 2097148 2096352 796
MemAvailable: 91638016 kB

shared : Memory used (mostly) by tmpfs (Shmem in /proc/meminfo, available on kernels 2.6.32, displayed as zero if not available)

total = used + free + buff/cache

available = free + buff/cache(部分)

el7中free的available其实对应:cat /proc/meminfo | grep MemAvailable

Estimation of how much memory is available for starting new applications, without swapping. Unlike the data provided

by the cache or free fields, this field takes into account page cache and also that not all reclaimable memory slabs

will be reclaimed due to items being in use (MemAvailable in /proc/meminfo, available on kernels 3.14, emulated on

kernels 2.6.27+, otherwise the same as free)

理解buffer和cache

我们可以使用dd命令去测试

首先生成一个1G的大文件

[root@jiangyi02.sqa.zmf /home/ahao.mah]
#dd if=/dev/zero of=bigfile bs=1M count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 1.71586 s, 611 MB/s
[root@jiangyi02.sqa.zmf /home/ahao.mah]
#du -sh bigfile
1001M bigfile

清空缓存

[root@jiangyi02.sqa.zmf /home/ahao.mah]
#echo 3 | tee /proc/sys/vm/drop_caches
3
[root@jiangyi02.sqa.zmf /home/ahao.mah]
#free -m
total used free shared buffers cached
Mem: 96839 1695 95144 0 6 46
-/+ buffers/cache: 1642 95196
Swap: 2047 0 2047

读入这个文件,测试消耗的时间

[root@jiangyi02.sqa.zmf /home/ahao.mah]
#time cat bigfile > /dev/null real 0m6.770s
user 0m0.005s
sys 0m0.477s
[root@jiangyi02.sqa.zmf /home/ahao.mah]
#free -m
total used free shared buffers cached
Mem: 96839 2709 94130 0 10 1051
-/+ buffers/cache: 1647 95192
Swap: 2047 0 2047

再次读入该文件,测试消耗的时间

[root@jiangyi02.sqa.zmf /home/ahao.mah]
#time cat bigfile > /dev/null real 0m0.235s
user 0m0.005s
sys 0m0.230s

对比,有了cache缓存后,第二次读的速度提高了28倍,这就是cache的力量

[root@jiangyi02.sqa.zmf /home/ahao.mah]
#echo "scale=3;6770/235" |bc
28.808

Free中的buffer和cache理解的更多相关文章

  1. 性能测试必备知识(11)- 怎么理解内存中的Buffer和Cache?

    做性能测试的必备知识系列,可以看下面链接的文章哦 https://www.cnblogs.com/poloyy/category/1806772.html 缓存 从 free 命令可以看到,缓存其实就 ...

  2. Linux操作系统中内存buffer和cache的区别--从free命令说起(转)

    原文链接:http://os.51cto.com/art/200709/56603.htm 我们一开始,先从Free命令说起. Free free 命令相对于top 提供了更简洁的查看系统内存使用情况 ...

  3. Linux内存中的 buffer 和 cache 到底是个什么东东?

    Linux 中的 free 命令,会输出: total 总量 used  已使用 free 空闲 shared 共享内存 buffers cached 前面四项都比较好理解,一看我也就知道啥意思了.但 ...

  4. Linux中的Buffer 与 Cache

    A buffer is something that has yet to be "written" to disk.       A cache is something tha ...

  5. 内存中的Buffer和Cache的区别

    Reference:https://time.geekbang.org/column/article/74633 磁盘是一个块设备,可以划分为不同的分区:在分区之上再创建文件系统,挂载到某个目录,之后 ...

  6. linux中内存使用,swap,cache,buffer的含义总结

    首先介绍一下linux中内存是如何使用的.当有应用需要读写磁盘数据时,由系统把相关数据从磁盘读取到内存,如果物理内存不够,则把内存中的部分数据导入到磁盘,从而把磁盘的部分空间当作虚拟内存来使用,也称为 ...

  7. linux free命令中buffer与cache的区别

    linux free命令中buffer与cache的区别 2012-05-15      个评论       收藏    我要投稿 linux free命令中buffer与cache的区别   ~$ ...

  8. free中buffer 与 cache 的区别

    通常人们所说的Cache就是指缓存SRAM. SRAM叫静态内存,“静态”指的是当我们将一笔数据写入SRAM后,除非重新写入新数据或关闭电源,否则写入的数据保持不变. 由于CPU的速度比内存和硬盘的速 ...

  9. 【Linux】基于Linux的buffer和cache学习

    缓存(cached)是把读取过的数据保存起来,重新读取时若命中(找到需要的数据)就不要去读硬盘了,若没有命中就读硬盘.其中的数据会根据读取频率进行组织,把最频繁读取的内容放在最容易找到的位置,把不再读 ...

随机推荐

  1. JBoss AS6 的服务状态说明

    (本文源码版本号为JBoss-AS-Final 6.1.0) JBoss 的服务状态定义在 LifecycleState 类中. 一共同拥有八个状态:INSTANCIATED, PRE_INIT, I ...

  2. Python3基础(六) 深入list列表

    正如Python FAQ1附录中说的, Python中任何值都是一个对象,所以任何类型(int.str.list-)都是一个类.而类就必然有它的方法或属性,我们要记下这么多类的所有方法显然是不可能的, ...

  3. 表格属就用treegrid

    http://maxazan.github.io/jquery-treegrid/ 如果想ajax后台动态添加表格数据然后再形成treegrid,那么可以通过后台给一个对应行索引的数组, 进行动态改变 ...

  4. 解决华为手机不出现logcat日志的问题

    问题描写叙述:公司一部华为手机在连接Eclipse时在Logcat中看不到相关日志 解决方法:1 进入手机拨号界面2 输入*#*#2846579#*#*3 输入完成后自己主动跳转到測试界面4 依次选择 ...

  5. 云上kafka和自建kafka对比

    说起Kafka,许多使用者对它是又爱又恨.Kafka是一种分布式的.基于发布/订阅的消息系统,其极致体验让人欲罢不能,但操心的运维.复杂的安全策略.可靠性易用性的缺失.算不上极致的性能发挥.并不丰富的 ...

  6. SQL SERVER:一条SQL语句插入多条记录等

    在学习排名第二的mySql过程中,发现它的插入语句可以这样写: use test; create table fruits( fid char(10) not null ,s_id int null ...

  7. css list menu

    选择让page和folder都显示出来

  8. Quartz.net使用入门(三)

    Windows服务,自定义安装,卸载服务+Quartz.net app.config配置文件 <?xml version="1.0"?> <configurati ...

  9. MSP430:PWM产生

    #define     PWM                      BIT6 //  Description: This program generates one PWM output on ...

  10. jQuery Validate插件实现表单强大的验证功能

    转自:http://www.jb51.net/article/76595.htm jQuery Validate插件捆绑了一套有用的验证方法,包括 URL 和电子邮件验证,同时提供了一个用来编写用户自 ...