The standard files/tools that report memory seem to have different formats on different Linux distributions. For example, on Arch and Ubuntu.
Arch
$ free total used free shared buff/cache available Mem: 8169312 3870392 2648348 97884 1650572 4110336 Swap: 16777212 389588 16387624 $ head /proc/meminfo MemTotal: 8169312 kB MemFree: 2625668 kB MemAvailable: 4088520 kB Buffers: 239688 kB Cached: 1224520 kB SwapCached: 17452 kB Active: 4074548 kB Inactive: 1035716 kB Active(anon): 3247948 kB Inactive(anon): 497684 kB
Ubuntu
$ free total used free shared buffers cached Mem: 80642828 69076080 11566748 3063796 150688 58358264 -/+ buffers/cache: 10567128 70075700 Swap: 20971516 5828472 15143044 $ head /proc/meminfo MemTotal: 80642828 kB MemFree: 11565936 kB Buffers: 150688 kB Cached: 58358264 kB SwapCached: 2173912 kB Active: 27305364 kB Inactive: 40004480 kB Active(anon): 7584320 kB Inactive(anon): 4280400 kB Active(file): 19721044 kB
So, how can I portably (across Linux distros only) and reliably get the amount of memory—excluding swap—that is available for my software to use at a particular time? Presumably that's what's shown as "available" and "MemAvailable" in the output of free
and cat /proc/meminfo
in Arch but how do I get the same in Ubuntu or another distribution?
awk -v low=$(grep low /proc/zoneinfo | awk '{k+=$2}END{print k}') '{a[$1]=$2}END{m=a["MemFree:"]+a["Active(file):"]+a["Inactive(file):"]+a["SReclaimable:"]; print a["MemAvailable:"],m-low}' /proc/meminfo
which should give me the same number printed twice. However, the second number (my understanding of the algorithm you suggest) is higher than theMemAvailable
shown in/proc/meminfo
. What am I doing wrong? – terdon Feb 10 '16 at 13:22/proc/zoneinfo
counts pages, which are mostly 4KB in size onamd64
; you're also missing the extra safety added to the page cache and the reclaimable memory. Simplifying the latter, we can subtract the low watermark three times, som-12*low
(3 × 4KB) gives the correct result on my system. (This simplification underestimates available memory if the page cache or reclaimable memory is smaller than twice the low watermark, but you wouldn't want to use much memory in that situation anyway so that seems to be a reasonable compromise.) – Stephen Kitt Feb 10 '16 at 13:31(file)
entires or theSReclaimable
entry? On an older centos box with kernel v 2.6.18-348.16.1.el5xen (peruname -r
) this is the output I get: http://pastebin.com/iFWiM1kX . Your calculation only pulls theMemFree
part – Mitch Nov 10 '16 at 19:07I know it is a late question, but can somebody please help me in understanding the significance of 12LOW. (So I read that the simplification is 4kb3). I didn't understand the multiple of 3.
What are these "extra safety added to the page cache and the reclaimable memory" ?
– Vipin Menon Aug 27 '20 at 13:48