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

时间:2022-01-07 08:31:02

I am writing a shell script that needs to know the total amount of available (free + reclaimable) memory in a system. For this I am parsing the output of the free command. The typical output of free is as follows:

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

It is common to consider that the "free" column, corrected by buffers and cache, represents memory that is either free or reclaimable, thus available for applications. So in this example above we would have approx 2124 MB available.

通常认为由缓冲区和缓存校正的“*”列表示可以*或可回收的存储器,因此可用于应用程序。因此,在上面的示例中,我们将有大约2124 MB可用。

However this is not correct if tmpfs is being used, as any memory used by tmpfs is included in "cached", yet this memory is not reclaimable (more info in this article)

但是,如果正在使用tmpfs,这是不正确的,因为tmpfs使用的任何内存都包含在“缓存”中,但此内存不可回收(本文中有更多信息)

How then can we find out the amount of memory that is actually available?

那么我们怎样才能找出实际可用的内存量?

1 个解决方案

#1


0  

Looks like getting the amount of available memory is not as easy as "free + buffers + cached - shmem". To solve this problem, Linux kernel 3.14 has introduced a new metric called "MemAvailable" which takes multiple factors into account:

看起来获得可用内存量并不像“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.

目前,可以从MemFree,Active(文件),Inactive(文件)和SReclaimable估计新工作负载可用的内存量,而无需将系统推入交换,以及来自/的“低”水印PROC / zoneinfo中。

More info can be found in the kernel commit message.

可以在内核提交消息中找到更多信息。

For kernels older than 3.14, there are tools that can emulate this metric in the same way as it is computed by the kernel.

对于早于3.14的内核,有些工具可以按照内核计算的方式模拟此度量标准。

#1


0  

Looks like getting the amount of available memory is not as easy as "free + buffers + cached - shmem". To solve this problem, Linux kernel 3.14 has introduced a new metric called "MemAvailable" which takes multiple factors into account:

看起来获得可用内存量并不像“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.

目前,可以从MemFree,Active(文件),Inactive(文件)和SReclaimable估计新工作负载可用的内存量,而无需将系统推入交换,以及来自/的“低”水印PROC / zoneinfo中。

More info can be found in the kernel commit message.

可以在内核提交消息中找到更多信息。

For kernels older than 3.14, there are tools that can emulate this metric in the same way as it is computed by the kernel.

对于早于3.14的内核,有些工具可以按照内核计算的方式模拟此度量标准。