Several resources around the net (apologies for not having links handy) recommend to determine the total amount of available physical memory on a Linux system by the second value on the "-/+ buffers/cache" line in the output of free
, with the explanation that buffers and cache may be discarded immediately if a process tries to allocate more memory than is unused at the time ("Mem:" line of free
). I've found this statement to be consistently empirically wrong, meaning allocating more memory than is "unused" fails frequently.
Researching about this, I've found the "MemAvailable:" line in /proc/meminfo
, and this explanation:
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.
My problem is that the system available for me to run my programs on has a 3.2.0 kernel which apparently does not provide "MemAvailable".
My question: How can I (approximately) compute the amount of available physical memory myself, using that memory information that is available to me?
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
PS: I realize the actual computation is detailed in the kernel patch I linked to, but I'm having a hard time relating this to the output that is available to me.
Answer
Here's a Perl script that estimates MemAvailable for kernel versions older than 3.14:
https://github.com/famzah/linux-memavailable-procfs
It replicates the same calculations done in the kernel, but using only info available to user-space in pre-3.14 kernels.
No comments:
Post a Comment