Linux – SNMP memory values do not match `free`

linuxmemory usagenet-snmp

Compare this

# free -m
             total       used       free     shared    buffers     cached
Mem:         72363      68035       4328          0        522      66294
-/+ buffers/cache:       1218      71145
Swap:        12291          0      12291

and this:

# snmpwalk -c public -v 2c localhost .1.3.6.1.4.1.2021.4
UCD-SNMP-MIB::memIndex.0 = INTEGER: 0
UCD-SNMP-MIB::memErrorName.0 = STRING: swap
UCD-SNMP-MIB::memTotalSwap.0 = INTEGER: 12586888 kB
UCD-SNMP-MIB::memAvailSwap.0 = INTEGER: 12586784 kB
UCD-SNMP-MIB::memTotalReal.0 = INTEGER: 74100516 kB
UCD-SNMP-MIB::memAvailReal.0 = INTEGER: 4429580 kB
UCD-SNMP-MIB::memTotalFree.0 = INTEGER: 17016364 kB
UCD-SNMP-MIB::memMinimumSwap.0 = INTEGER: 16000 kB
UCD-SNMP-MIB::memBuffer.0 = INTEGER: 534804 kB
UCD-SNMP-MIB::memCached.0 = INTEGER: 44238560 kB
UCD-SNMP-MIB::memSwapError.0 = INTEGER: noError(0)
UCD-SNMP-MIB::memSwapErrorMsg.0 = STRING:

Why does free show 66294MB for "cache" while snmp shows about 44238MB for "memCached"? Shouldn't that be the same?

Looking at the MIB I see that the "memCached" is "physical or virtual" memory used for caching. (Don't tell me it puts disk cache into swap) ^^

The goal is finding out the real free physical memory (i.e. here 71145 as shown by free) via snmp.


More info

# cat /proc/meminfo
MemTotal:     74100516 kB
MemFree:       4422092 kB
Buffers:        542168 kB
Cached:       44239460 kB
SwapCached:          4 kB
Active:       16455504 kB
Inactive:     28707308 kB
SwapTotal:    12586888 kB
SwapFree:     12586784 kB
Dirty:            2536 kB
Writeback:           0 kB
AnonPages:      381088 kB
Mapped:         252132 kB
Slab:         23961488 kB
SReclaimable: 23648768 kB
SUnreclaim:     312720 kB
PageTables:       7812 kB
NFS_Unstable:        0 kB
Bounce:              0 kB
WritebackTmp:        0 kB
CommitLimit:  49637144 kB
Committed_AS:        4 kB
VmallocTotal: 34359738367 kB
VmallocUsed:    264124 kB
VmallocChunk: 34359474191 kB
HugePages_Total:     0
HugePages_Free:      0
HugePages_Rsvd:      0
HugePages_Surp:      0
Hugepagesize:     2048 kB
DirectMap4k:      7936 kB
DirectMap2M:  75481088 kB

Best Answer

From the manpage for free(1):

cache Memory used by the page cache and slabs (Cached and Slab in /proc/meminfo)

free includes slab allocation in cache; UCD-SNMP-MIB doesn't. If you add in the slab allocation, you get:

UCD-SNMP-MIB::memCached.0 + slab = 44238560 + 23961488
                                 = 68200048 KB

which is much closer to what free reported (67885056 KB).


As for getting real free physical memory, the best you can do with this MIB is get a rough estimate:

totFree = memAvailReal.0 + memBuffer.0 + memCached.0
        = 4429580        + 534804      + 44238560
        = 49202944 KB

which is still significantly lower than the value of free + buffers/cache reported by free.

Note that the HOST-RESOURCES-MIB is no better; see my answer to a similar question on Stack Overflow.


Newer kernels actually provide a better metric for this. Again from the free(1) manpage:

available

Estimation of how much memory is available for starting new applications, without swapping. Unlike the data provided by the cache or free fields, this field takes into account page cache and also that not all reclaimable memory slabs will be reclaimed due to items being in use (MemAvailable in /proc/meminfo, available on kernels 3.14, emulated on kernels 2.6.27+, otherwise the same as free)

This value will be lower than the total of free + buffers/cache and would be a better gauge of available memory, but I don't see it in your /proc/meminfo and I haven't found a MIB that uses it.