如何估计 Linux 上可用的物理内存?

如何估计 Linux 上可用的物理内存?

网络上的多个资源(抱歉没有提供链接)建议通过 输出中“-/+ buffers/cache”行的第二个值来确定 Linux 系统上可用的物理内存总量free,并解释说,如果进程试图分配比当时未使用的内存更多的内存,则缓冲区和缓存可能会立即被丢弃(“Mem:”行free)。我发现这个说法在经验上一直是错误的,这意味着分配比“未使用”更多的内存经常会失败。

研究这个问题时,我在 中找到了“MemAvailable:”这一行/proc/meminfo,并且这个解释

Subject: provide estimated available memory in /proc/meminfo

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.

我的问题是,我可以在运行程序的系统上安装 3.2.0 内核,显然它不提供“MemAvailable”。

我的问题:我如何使用可用的内存信息自己(近似地)计算可用的物理内存量?

MemTotal:       16536344 kB
MemFree:         8615224 kB
Buffers:          241896 kB
Cached:          1341944 kB
SwapCached:       100232 kB
Active:          5320768 kB
Inactive:        2021312 kB
Active(anon):    4298508 kB
Inactive(anon):  1483164 kB
Active(file):    1022260 kB
Inactive(file):   538148 kB
Unevictable:          32 kB
Mlocked:              32 kB
SwapTotal:       9918460 kB
SwapFree:        7730432 kB
Dirty:                56 kB
Writeback:             0 kB
AnonPages:       5720696 kB
Mapped:           103628 kB
Shmem:             23432 kB
Slab:             432516 kB
SReclaimable:     356780 kB
SUnreclaim:        75736 kB
KernelStack:        7320 kB
PageTables:        69764 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:    18186632 kB
Committed_AS:   12325296 kB
VmallocTotal:   34359738367 kB
VmallocUsed:      120924 kB
VmallocChunk:   34351227352 kB
HardwareCorrupted:     0 kB
AnonHugePages:         0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:      103240 kB
DirectMap2M:    16738304 kB

附言:我意识到实际计算在链接到的内核补丁中有详细说明,但我很难将其与我可用的输出联系起来。

答案1

下面是一个 Perl 脚本,用于估算内核版本 3.14 之前的 MemAvailable:

https://github.com/famzah/linux-memavailable-procfs

它复制了内核中完成的相同计算,但仅使用 3.14 之前的内核中用户空间可用的信息。

相关内容