How can I get the amount of available memory portably across distributions?
16
votes
2
answers
16799
views
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?
Asked by terdon
(251585 rep)
Feb 10, 2016, 12:53 PM
Last activity: Oct 16, 2020, 03:46 PM
Last activity: Oct 16, 2020, 03:46 PM