linux - Linux 中应用程序可用的内存总量

标签 linux shell memory

我正在编写一个 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/

相关文章:

Linux shell 脚本 : make a folder with the current date name

c - 在多个函数中使用全局变量后发生段错误

linux - systemctl disable name.service 在重启后不会持续存在

arrays - 如何在 Linux shell 中从文件中的二维数组中选择一个元素

linux - 从 Linux 备份到 Windows Server

linux - 从父目录在同一上下文中调用 bash 脚本

shell - 比较同一文件中的列以在同一文件中再插入一列

c - 定义linux中C程序允许的最大char[]数组大小

c++ - 32 位代码中 DWORD 与 QWORD 对齐的性能

java - 来自配置文件或 .java 的巨大数组