我正在编写一个 shell 脚本,它需要知道系统中可用(空闲 + 可回收)内存的总量。为此,我正在解析 free
命令的输出。 free
的典型输出如下:
$ free -m
total used free shared buffers cached
Mem: 2488 965 1523 0 83 517
-/+ buffers/cache: 363 2124
Swap: 1565 0 1565
通常认为“空闲”列,由缓冲区和缓存更正,表示可用或可回收的内存,因此可用于应用程序。因此,在上面的示例中,我们将有大约 2124 MB 可用。
但是,如果正在使用 tmpfs,这是不正确的,因为 tmpfs 使用的任何内存都包含在“缓存”中,但该内存不可回收(更多信息见 this article)
那么我们如何才能知道实际可用的内存量呢?
最佳答案
看起来获得可用内存量并不像“free + buffers + cached - shmem”那么容易。为了解决这个问题,Linux 内核 3.14 引入了一个名为“MemAvailable”的新指标,它考虑了多个因素:
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.
更多信息可以找到in the kernel commit message .
对于早于 3.14 的内核,there are tools可以以与内核计算相同的方式模拟此指标。
关于linux - Linux 中应用程序可用的内存总量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27525672/