简体   繁体   中英

Total amount of memory available for applications in Linux

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:

$ 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.

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 )

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

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:

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM