吐血推荐文章: 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. Raphael.js image 在ie8以下的兼容性问题

    Raphael.js 在ie7,ie8浏览器内绘制图形採用的vml,在绘制image的时候会解析成 <?xml:namespace prefix = "rvml" ns = ...

  2. 关于Tomcat的点点滴滴(体系架构、处理http请求的过程、安装和配置、文件夹结构、设置压缩和对中文文件名称的支持、以及Catalina这个名字的由来……等)

    总结Tomcat的体系架构.处理http请求的过程.安装和配置.文件夹结构.设置压缩和对中文文件名称的支持.以及Catalina这个名字的由来--等. Tomcat和JVM: 一个Tomcat仅仅会启 ...

  3. The Pragmatic Programmer 读书笔记之中的一个 DRY-Don’t Repeat Youself

     The Pragmatic Programmer读书笔记之中的一个 DRY-Don't Repeat Youself 尽管自己买了非常多软件project方面的书,可是由于时间的问题.一直没有静 ...

  4. luogu 3834 【模板】可持久化线段树 1(主席树)

    我这种菜鸡还是%一下棒神比较好 #include<iostream> #include<cstdio> #include<cmath> #include<cs ...

  5. [置顶] Snow的追寻

    题目描述 Snow终于得知母亲是谁,他现在要出发寻找母亲. 王国中的路由于某种特殊原因,成为了一棵有n个节点的根节点为1的树,但由于"Birds are everywhere.", ...

  6. Spark 操作Hive 流程

    1.ubuntu 装mysql 2.进入mysql: 3.mysql>create database hive (这个将来是存 你在Hive中建的数据库以及表的信息的(也就是元数据))mysql ...

  7. hibernate类或方法---备忘录

  8. Behavior Designer扩展

    BehaviorManager.instance.Tick(behaviorTree); 卸载update里u3d直接卡死 = = SharedVariable直接赋值会改变他的引用关系,必须用XXX ...

  9. flask中的蓝图(BluePrint)

    蓝图,听起来就是一个很宏伟的东西 在Flask中的蓝图 blueprint 也是非常宏伟的 它的作用就是将 功能 与 主服务 分开怎么理解呢? 比如说,你有一个客户管理系统,最开始的时候,只有一个查看 ...

  10. ACM_Power Mouth

    Power Mouth Time Limit: 2000/1000ms (Java/Others) Problem Description: Your task is to calculate the ...