I’ve given another try to understand the Linux memory usage statistics which are exported by “/proc/meminfo“. The main reason was to know how much memory is still available for free allocation by user applications, and also to understand the memory usage of the different Linux subsystems like the kernel, file system cache, etc.
The whole analysis is done on 649 machines with different workload. Most of them are running a 64-bit Linux kernel 3.2.59, while the others are running a 64-bit kernel with versions between 3.2.42 and 3.2.60. This is an LTS Linux kernel.
Cached memory and tmpfs
One of the most astonishing facts that I found out was that the “tmpfs” in-memory file system is counted against the “Cached” value in “/proc/meminfo”. The “tmpfs” file system is commonly mounted in “/dev/shm”. There is a separate stats key “Shmem” which shows the amount of memory allocated to “tmpfs” files, but at the same time this memory is added to the value of “Cached”. This is rather confusing because we’re used to think that both “Cached” and “Buffers” are reclaimable memory which can be freed anytime (or very soon enough) for use by applications or the kernel. That’s the current behavior of the “free” memory statistics tool which is widely used by System administrators to get an overview of the memory usage on their Linux systems. The “free” tool is part of the “procps” package which as of today is still not fixed and showing the “Cached” memory as free.
Here is a little proof:
famzah@vbox:~$ free -m total used free shared buffers cached Mem: 2488 965 1523 0 83 517 -/+ buffers/cache: 363 2124 Swap: 1565 0 1565 famzah@vbox:~$ dd if=/dev/zero of=/dev/shm/bigfile bs=1M count=800 838860800 bytes (839 MB) copied, 0.53357 s, 1.6 GB/s famzah@vbox:~$ free -m total used free shared buffers cached Mem: 2488 1765 723 0 83 1317 -/+ buffers/cache: 364 2124 Swap: 1565 0 1565
The “cached” memory got 800 MB bigger. The free “-/+ buffers/cache” memory is still the same amount (2124 MB), which is wrong because the used memory in “tmpfs” cannot be reclaimed nor given to user applications or the kernel.
The reason that “tmpfs” is added to the “cached” memory usage is because “tmpfs” is implemented in the page cache which is accounted in the “cached” stats.
Active, Inactive, and Slab
The following is true for all machines that I tested on:
- Active = Active(anon) + Active(file)
- Inactive = Inactive(anon) + Inactive(file)
- Slab = SReclaimable + SUnreclaim
Total memory usage
I tried to sum up the usage of the different fields in “/proc/meminfo” up to the maximum amount of available memory on each machine. The following two formulas both represent the whole memory usage:
- MemTotal = MemFree + (Buffers + Cached + SwapCached) + AnonPages + (Slab + PageTables + KernelStack)
- MemTotal = MemFree + (Active + Inactive) + (Slab + PageTables + KernelStack)
Both those formulas can be expanded to include the sub-components as well:
- MemTotal = MemFree + (Buffers + Cached + SwapCached) + AnonPages + ((SReclaimable + SUnreclaim) + PageTables + KernelStack)
- MemTotal = MemFree + ((“Active(anon)” + “Active(file)”) + (“Inactive(anon)” + “Inactive(file)”)) + ((SReclaimable + SUnreclaim) + PageTables + KernelStack)
The formulas give an error of up to -3% for only 6 machines. For the rest of the machines the error is less than +/- 1.5%. The less total available memory a machine has, the bigger the error. I suppose that this small error comes by the fact that not all “/proc/meminfo” counters are updated atomically at once.
The following keys in “/proc/meminfo” seem to be accounted into other super-set keys, and are therefore only for statistical purpose:
- Shmem — accounted in “Cached”
- Linux kernel docs — the /proc file system
- Stackoverflow — unevictable page
- Linux kernel docs — unevictable LRU infrastructure
- stackexchange.com — Tracking down “missing” memory usage in linux
- linux-mm.org — Low On Memory
- Stackoverflow — Linux total available memory
- novell.com — Bug 405246 – Free memory reported by free, vmstat and top is wrong
- LINUX-VM mailing list — Amount of memory in buffers shown by the free command
- A fun image which represents the Linux Memory Management 🙂