CentOS Linux 常用命令整理( free -m 命令的解释说明,更新于 2018 年 9 月 6 日)

更多 CentOS Linux 常用命令整理请点击这里

Ricky 发现不少朋友都看不懂 free -m 命令的输出结果,在这里做一个解释说明:

free 命令选项:

  • -b :以 Byte 为单位显示内存使用情况;
  • -k :以 KB 为单位显示内存使用情况;
  • -m :以 MB 为单位显示内存使用情况;
  • -o :不显示缓冲区调节列;
  • -s<间隔秒数> :持续观察内存使用状况;
  • -t :显示内存总和列;
  • -V :显示版本信息。

free -m 命令输出结果的样式汇总:

目前在主流版本的 CentOS Linux 上 free -m 命令的输出结果有两种样式:

1、如 CentOS Linux 5.8 、6.2 、6.9 和 7.0 上的样式:

[root@host ~]# cat /etc/redhat-release 
CentOS release 5.8 (Final)
[root@host ~]# 
[root@host ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          7982       7933         48          0         53       7329
-/+ buffers/cache:        551       7431
Swap:         1027          0       1027
[root@host ~]# 

[root@host ~]# cat /etc/redhat-release 
CentOS release 6.2 (Final)
[root@host ~]# 
[root@host ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          7873       5919       1953          0        301        672
-/+ buffers/cache:       4945       2927
Swap:         1023          0       1023
[root@host ~]#

[root@host ~]# cat /etc/redhat-release 
CentOS release 6.9 (Final)
[root@host ~]# 
[root@host ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          3948       1286       2661          4        376        434
-/+ buffers/cache:        475       3472
Swap:         1023          0       1023
[root@host ~]# 

[root@host ~]# cat /etc/redhat-release 
CentOS Linux release 7.0.1406 (Core) 
[root@host ~]# 
[root@host ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          7824       3549       4274        240          0       1725
-/+ buffers/cache:       1823       6000
Swap:         1023          0       1023
[root@host ~]#

2、如 CentOS Linux 7.5 上的样式:

[root@host ~]# cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core) 
[root@host ~]# 
[root@host ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           7821         183        7486           8         152        7417
Swap:          1023           0        1023
[root@host ~]#

free -m 命令的参数说明与计算:

1、以第一个样式中的 CentOS 6.9 的输出结果为例:

[root@host ~]# cat /etc/redhat-release 
CentOS release 6.9 (Final)
[root@host ~]# 
[root@host ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          3948       1286       2661          4        376        434
-/+ buffers/cache:        475       3472
Swap:         1023          0       1023
[root@host ~]#

参数说明:

  • Mem :表示物理内存的使用情况。
  • -/+ buffers/cache :表示物理内存中缓存的使用情况。
  • Swap :表示硬盘上交换分区的使用情况,相当于 Windows 上的虚拟内存,即把一部分硬盘空间当做内存来使用。
  • total :总量。
  • used :使用量。
  • free :空闲量。
  • shared :共享内存,主要用在 Unix 环境下不同进程之间共享数据,是进程间通信的一种方法。
  • buffers :A buffer is something that has yet to be ” written ” to disk.( buffers 缓存,一个用于存储速度不同步的设备或优先级不同的设备之间传输数据的区域;通过缓冲区可以使进程之间的相互等待变少,从而使从速度慢的设备读入数据时速度快的设备的操作进程不发生间断;也可把分散的写操作集中进行,减少磁盘碎片和磁盘的反复寻道,从而提高系统性能;也可以通过 sync 命令手动清空缓存)。
  • cached :A cache is something that has been ” read ” from the disk and stored for later use.( cached 缓存,把读取过的数据保存起来,重新读取时若命中就不用去读硬盘了,若没有命中就读硬盘;其中的数据会根据读取频率进行组织,把最频繁读取的内容放在最容易找到的位置,把不再读的内容不断往后排,直至从中删除)。

关于 buffers 我们举个例子:现在有一块 ext4 的 U 盘,此时往里面 cp 一个 3 MB 的 MP3 格式的文件,但 U 盘的灯没有跳动(其实文件写入到了 buffers ),过了一会儿(或者手工执行 sync 命令)U 盘的灯就跳动起来了(文件正式写入到 U 盘);卸载设备时会清空缓存,所以有些时候卸载一个设备需要等上几秒钟(将所有的缓存数据全部写入到 U 盘)。

先来说说各名词:

[root@host ~]# cat /etc/redhat-release 
CentOS release 6.9 (Final)
[root@host ~]# 
[root@host ~]# free -m
             total       used       free     shared    buffers     cached
Mem:         位置一      位置二      位置三      位置四      位置五      位置六
-/+ buffers/cache:      位置七      位置八
Swap:        位置九      位置十    位置十一
[root@host ~]#
  • 位置一:物理内存总量
  • 位置二:已分配的物理内存
  • 位置三:未分配的物理内存
  • 位置四:共享内存
  • 位置五:buffers 缓存
  • 位置六:cached 缓存
  • 位置七:系统和应用程序使用的内存(这里面已经包含了共享内存)
  • 位置八:可使用的物理内存
  • 位置九:交换分区总量
  • 位置十:交换分区使用量
  • 位置十一:交换分区空闲量

除了位置七和位置八以外,这些名词都是很好理解的。

[root@host ~]# cat /etc/redhat-release 
CentOS release 6.9 (Final)
[root@host ~]# 
[root@host ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          3948       1286       2661          4        376        434
-/+ buffers/cache:        475       3472
Swap:         1023          0       1023
[root@host ~]#

由上例可知:

(第一行)

  • 物理内存总量为 3948 MB ;
  • 已分配的物理内存为 1286 MB ;
  • 未分配的物理内存为 2661 MB ;
  • 共享内存使用了 4 MB ;
  • buffers 缓存使用了 376 MB ;
  • cached 缓存使用了 434 MB 。

(第二行)

  • 系统和应用程序使用的内存(这里面已经包含了共享内存)使用了 475 MB ;
  • 可使用的物理内存为 3472 MB 。

(第三行)

  • 交换分区总量为 1023 MB ;
  • 交换分区使用量为 0 MB ;
  • 交换分区空闲量为 1023 MB 。

物理内存总量 = 已分配的物理内存 + 未分配的物理内存,即 1286 MB + 2661 MB = 3947 MB(约等于 3948 MB ,这里之所以是约等于是因为内存往往都是以字节为单位进行分配的,如果以 MB 为单位来显示的话就会出现四舍五入的问题)。

交换分区总量 = 交换分区使用量 + 交换分区空闲量,即 0 MB + 1023 MB = 1023 MB 。

[root@host ~]# cat /etc/redhat-release 
CentOS release 6.9 (Final)
[root@host ~]# 
[root@host ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          3948       1286       2661          4        376        434
-/+ buffers/cache:        475       3472
Swap:         1023          0       1023
[root@host ~]#

已分配的物理内存 = 系统和应用程序使用的内存(这里面已经包含了共享内存)+ buffers 缓存 + cached 缓存,即 475 MB + 376 MB + 434 MB = 1285 MB(约等于 1286 MB )。

换句话说 used 这一列:

  • 1286 MB 表示实际已分配出去的物理内存(其中包含了共享内存、buffers 缓存和 cached 缓存等);
  • 475 MB 表示实际已分配出去的物理内存减去 buffers 缓存和 cached 缓存(即系统和应用程序使用的内存,其中包含了共享内存)。
[root@host ~]# cat /etc/redhat-release 
CentOS release 6.9 (Final)
[root@host ~]# 
[root@host ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          3948       1286       2661          4        376        434
-/+ buffers/cache:        475       3472
Swap:         1023          0       1023
[root@host ~]#

同样,未分配的物理内存 = 物理内存总量 – 已分配的物理内存,即 3948 MB – 1286 MB = 2662 MB(约等于 2661 MB )。

buffers 缓存和 cached 缓存是可以被清理掉的,详情请点击这里

如果把 buffers 缓存和 cached 缓存当做可使用的物理内存来看待(毕竟缓存可清理),那么可使用的物理内存 = 未分配的物理内存 + buffers 缓存 + cached 缓存,即 2661 MB + 376 MB + 434 MB = 3471 MB(约等于 3472 MB )。

也可以这么计算,可使用的物理内存 = 物理内存总量 – 系统和应用程序使用的内存(这里面已经包含了共享内存),即 3948 MB – 475 MB = 3473 MB(约等于 3472 MB )。

换句话说 free 这一列:

  • 2661 MB 表示实际未分配出去的物理内存;
  • 3472 MB 表示实际未分配出去的物理内存加上 buffers 缓存和 cached 缓存(即可使用的物理内存,buffers 缓存和 cached 缓存这两部分空间是可以再利用的,也可以手动清理掉,所以是可以把它们当做可使用的物理内存来看待的)。

2、以第二个样式中的 CentOS 7.5 的输出结果为例:

[root@host ~]# cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core) 
[root@host ~]# 
[root@host ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           7821         183        7486           8         152        7417
Swap:          1023           0        1023
[root@host ~]#

参数说明:

  • Mem 、Swap 、total 、used 、free 、shared 、buff 和 cache 同上一个样式的说明。
  • available 是由程序自己估算出的可使用的物理内存。

先来说说各名词:

[root@host ~]# cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core) 
[root@host ~]# 
[root@host ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:         位置一       位置二       位置三       位置四      位置五      位置六
Swap:        位置七       位置八       位置九
[root@host ~]#
  • 位置一:物理内存总量
  • 位置二:系统和应用程序使用的内存(这里面已经包含了共享内存),不是已分配的物理内存
  • 位置三:未分配的物理内存
  • 位置四:共享内存
  • 位置五:buffers 缓存 + cached 缓存
  • 位置六:由程序自己估算出的可使用的物理内存
  • 位置七:交换分区总量
  • 位置八:交换分区使用量
  • 位置九:交换分区空闲量

新的输出结果有三个地方发生了改变:

  1. 位置二不再是已分配的物理内存,而是系统和应用程序使用的内存(这里面已经包含了共享内存);
  2. 位置五将 buffers 缓存和 cached 缓存进行了合并,可以统称为缓存;
  3. 位置六这个 “ 可使用的物理内存 ” 跟上一个样式中的 “ 可使用的物理内存 ” 不一样,这里是由程序自己估算出来的可使用的物理内存。

位置六:由程序自己估算出的可使用的物理内存,即 available 的官方解释如下:

/proc/meminfo: provide estimated available memory
Many load balancing and workload placing programs check /proc/meminfo to
estimate how much free memory is available.  They generally do this by
adding up "free" and "cached", which was fine ten years ago, but is
pretty much guaranteed to be wrong today.

It is wrong because Cached includes memory that is not freeable as page
cache, for example shared memory segments, tmpfs, and ramfs, and it does
not include reclaimable slab memory, which can take up a large fraction
of system memory on mostly idle systems with lots of files.

Currently, the amount of memory that is available for a new workload,
without pushing the system into swap, can be estimated from MemFree,
Active(file), Inactive(file), and SReclaimable, as well as the "low"
watermarks from /proc/zoneinfo.

However, this may change in the future, and user space really should not
be expected to know kernel internals to come up with an estimate for the
amount of free memory.

It is more convenient to provide such an estimate in /proc/meminfo.  If
things change in the future, we only have to change it in one place.

Signed-off-by: Rik van Riel <riel@redhat.com>
Reported-by: Erik Mouw <erik.mouw_2@nxp.com>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

根据第一个样式中的公式可得:

[root@host ~]# cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core) 
[root@host ~]# 
[root@host ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           7821         183        7486           8         152        7417
Swap:          1023           0        1023
[root@host ~]#

已分配的物理内存 = 系统和应用程序使用的内存(这里面已经包含了共享内存)+ buffers 缓存 + cached 缓存,即 183 MB + 152 MB = 335 MB 。

未分配的物理内存 = 物理内存总量 – 已分配的物理内存,即 7821 MB – 335 MB = 7486 MB 。

 

本文完。如有疑问,欢迎在下方留言;如本文有什么错误,欢迎在下方留言指正,谢谢。

参考自:

  • https://www.cnblogs.com/sunny3096/p/7474594.html
  • https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773

这篇文章对你有帮助吗?

相关文章

发表评论?

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据