合 Linux free命令介绍、buffer和cache的区别、缓存等
Tags: Linux监控命令介绍swapBuffer Cache缓存交换空间buffercachedrop_cachesfree释放Cache
- 简介
- free输出示例
- Linux 6下:
- Linux 7下:
- free命令的选项
- free 与 available 的区别
- buffer和cache的区别
- Page Cache
- Buffer Cache
- 释放Cache(回收cache)
- 为什么要清除 buffers 和caches?
- 如何手工释放Cache
- 示例
- 为什么echo 0 > /proc/sys/vm/drop_caches会报错
- cache都能被回收么?
- tmpfs
- 共享内存shm
- mmap
- 查看缓存命中率
- 如何查看 Linux 当前缓存了哪些大文件?
- 文件的缓存大小
- Linux系统cache使用频繁过大,如何让系统自动积极的清理cache
- 参数配置
- 脚本定期清理cache
- 交换空间(swap space)
- 总结
- 参考
简介
Linux free命令用于显示内存状态。
free指令会显示内存的使用情况,包括实体内存,虚拟的交换文件内存,共享内存区段,以及系统核心使用的缓冲区等。
free输出示例
Linux 6下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | [root@dbs ~]# free -m total used free shared buffers cached Mem: 193838 98861 94976 0 1 36914 -/+ buffers/cache: 61946 131891 Swap: 67583 43 67540 [root@dbs ~]# free -g total used free shared buffers cached Mem: 189 96 92 0 0 36 -/+ buffers/cache: 60 128 Swap: 65 0 65 [root@oadata ~]# free -g total used free shared buffers cached Mem: 63 60 2 0 0 53 -/+ buffers/cache: 7 55 Swap: 16 0 16 [root@lhrora11204 /]# free total used free shared buffers cached Mem: 25647072 1131728 24515344 11532 66604 469096 -/+ buffers/cache: 596028 25051044 Swap: 10485760 0 10485760 [root@lhrora11204 /]# free -k total used free shared buffers cached Mem: 25647072 1131728 24515344 11532 66604 469116 -/+ buffers/cache: 596008 25051064 Swap: 10485760 0 10485760 [root@lhrora11204 /]# |
free的默认显示单位是kb
在linux 6中,我们看到free命令有三行内容:Mem,-/+ buffers/cache,Swap,代表的内容如下:
名称 | 代表的内容 |
---|---|
Mem | 内存的使用情况。total代表总的内存大小, used 代表程序已经使用的内存大小,free代表的是空闲的内存大小, shared 代表的是共享内存,buffers 代表的是 buffers cache缓冲(即将要写入磁盘), cached 代表的是 Page cache缓存(从磁盘读取到内存). |
-/+ buffers/cache | 表示物理内存已用多少,可用多少。used 代表的除去buffers和cached实际程序占用的内存, free代表的是“ total - 第二行used”, 也就是 “第一行free + 第一行buffers + 第一行cached” |
Swap | 交换空间(虚拟内存)的使用情况 |
示例:
1 2 3 4 5 | [root@tencent64 ~]# free total used free shared buffers cached Mem: 132256952 72571772 59685180 0 1762632 53034704 -/+ buffers/cache: 17774436 114482516 Swap: 2101192 508 2100684 |
在以上这个系统中,物理内存128g,内存其实真实才用了17G左右,而不是70g。
Linux 7下:
centos7 的free 命令多了一个 -h 参数,它会自动根据内存的大小,自动转换成合适的单位,默认的计算倍数是 1024 。free 的显示信息来自于/proc/meminfo,常用参数:
-h 以人性化显示
--si 指定计算倍数为1000.
-b 指定单位为bytes
-m 指定单位为mb
-k 指定单位为kb,默认单位
-g 指定单位为gb
-t 显示物理内存和虚拟内存的总和
-s n 每隔n秒显示一次
-c 显示多少次
-V 显示free的版本信息并退出
-l 显示最低和最高的内存数据统计
-w 将buff和cacher 分开显示,默认是在一起显示的。
1 2 3 4 | [root@docker35 ~]# free -h total used free shared buff/cache available Mem: 44G 5.2G 298M 1.9G 39G 37G Swap: 10G 432K 10G |
先说明一些基本概念
第一列Mem
内存的使用信息Swap
交换空间的使用信息
第一行total
系统总的可用物理内存大小used
已被使用的物理内存大小free
还有多少物理内存可用shared
被共享使用的物理内存大小buff/cache
被 buffer 和 cache 使用的物理内存大小available
还可以被 应用程序 使用的物理内存大小
total 代表的总的内存 (对应/proc/meminfo 里的MemTotal)
used 代表的是 已经使用的内存
free 是代表空闲的内存(对应/proc/meminfo 里的MemFree)
shared 代表的是共享的内存(对应/proc/meminfo 里的Shmem)
buff/cache 代表的是 buff(对应/proc/meminfo里的Buffers)和cache的内存(对应/proc/meminfo 里的Cached and Slab),
available 代表的是 活跃的内存
(官方文档对于available这样写到: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)
) available 代表的是一个新的应用程序可以使用内存的估计值,它不等于 free + buff/cache ,因为在centos 7中并不是所有的buffer/cache空间都可以被回收。
free命令的选项
在linux环境下,使用man命令、--help命令、info命令可查询free命令的所有选项,以--help为例,如:
现在对free命令的所有选项分别进行解释:
注意:free命令的选项区分大小写,请认真对照上图中是大写还是小写
- free -b:以bytes为单位来显示内存的信息。
free -k:以kb为单位来显示内存的信息。
free -m:以m为单位来显示内存的信息。
free -g:以G为单位来显示内存的信息。
free -h:以适于人类可读方式显示内存信息。-h与其他命令最大不同是-h选项会在数字后面加上适于人类可读的单位
如我们一起看到的,在1,0数字后这里是G(GB)字母。当数字并没有达到GB时,free足够聪明来知道并在每个数字后面跟上合适的单位。后面的M - 数字929告诉我们它有929MB(译注: 原文为929 number tell us its 969 Megabytes,这里应该为输入错误)。
如果我们需要每列的总计信息,我们可以在 free 命令后面跟上 -t 选项。这会在字底部额外加入一行显示。
free命令的选项是可以组合的,举例如下
free -hs 3
: 以人类可读的方式,每隔3s打印一次内存信息,直到ctrl+c结束