Notes
Virtual memory
Every process sees its own virtual address space, as if it had the whole machine. Two numbers describe its memory. Virtual size counts everything it has reserved, which can be huge and mostly is not real memory. Resident set size, R S S, counts the pages actually in RAM right now. ShopRung, running, had forty eight thousand kilobytes resident, about forty seven megabytes, with seven threads. When you judge an app's memory, use RSS, not virtual size.
- VSZ / VIRT: address space reserved (can be huge, mostly not real memory)
- RSS / RES: pages really in RAM right now
- ShopRung (our run): VmRSS 48,172 kB, 7 threads
- judge memory by RSS, not VIRT
The page cache
Linux keeps recently read files in otherwise unused RAM, the page cache. We cleared the cache, then read a five hundred megabyte file twice. The first read came from disk and took three hundred and nine milliseconds. The second came from memory and took seventy six, four times faster. The Cached line in meminfo grew by about five hundred and twelve megabytes. Unused RAM is wasted RAM, so Linux fills it with cache.
drop caches, then read twice: read 1 (from disk) 309 ms read 2 (from RAM) 76 ms Cached before: 180,412 kB Cached after: 692,712 kB (+ about 512 MB)
Read free correctly
That is why free can look scary. The free column is memory doing nothing at all. Buff slash cache is the page cache, which the kernel hands back instantly when a program needs memory. The number that matters is available: an estimate of how much memory applications could still get without swapping. On our machine, seven thousand three hundred megabytes available out of eight thousand. Alert on available, never on free.
total used free
Mem: 8023 706 6851
shared buff/cache available
12 685 7316
free: totally unused
buff/cache: reclaimable cache
available: what apps can still
get without swapping Swap
Swap is disk space used as overflow memory. The kernel moves rarely used pages there to make room. A little swap keeps a machine alive under pressure, but heavy swapping makes everything slow, and vmstat's swap in and swap out columns show it. Our machine had no swap at all, which is common for cloud servers and Kubernetes nodes, where it is better to fail fast than to crawl. The swappiness setting controls how eagerly the kernel swaps.
- rarely used pages move to disk
- keeps the system alive under pressure, but much slower
- heavy swapping = everything slows: watch si/so in vmstat
- our machine: no swap
- Kubernetes nodes usually run without swap
- vm.swappiness: how eagerly
The OOM killer
When the kernel truly cannot find memory, it calls the out of memory killer. It scores processes, mostly by how much memory they use, adjusted by an O O M score setting, and kills the winner with SIGKILL. There is no cleanup and the exit code is one three seven, the same as in the processes episode. It is logged in the kernel log, so dmesg or journalctl minus k shows which process died and why.
- the kernel cannot free enough memory for a request
- it picks a victim by oom_score (big, unimportant)
- sends SIGKILL: no cleanup, exit code 137
- logged: dmesg, journalctl -k "Killed process ... (python3)"
Watch it
We watched it happen. We put a small Python program into a control group, a cgroup, limited to a hundred megabytes, and let it leak one megabyte at a time. It printed twenty five, fifty, seventy five megabytes, and then the shell printed Killed. Exit code one three seven. The cgroup recorded one OOM kill at exactly a hundred megabytes of usage, and dmesg said python three invoked the OOM killer. Only the program in the cgroup died; the rest of the machine was fine.
cgroup memory.limit = 100 MB python leaks 1 MB per step: 25 MB 50 MB 75 MB Killed exit=137 oom_kill 1 max usage 104,857,600 bytes dmesg: python3 invoked oom-killer
Why containers care
This is exactly how container limits work. Docker's memory flag, Kubernetes memory limits and systemd's memory max all create a cgroup like ours. When a container hits its limit, it is OOM killed, and Kubernetes shows the reason OOMKilled. The fix is either to find the leak, or to set the limit from a measured peak plus headroom. For Java and Node, also set the heap size below the limit, or the runtime will grow until the kernel kills it.
- docker run -m 512m → cgroup
- Kubernetes limits.memory → cgroup → OOMKilled
- systemd MemoryMax= → cgroup
- fix: find the leak, or give a real limit from measured peak + headroom
- Java, Node: set heap below the limit
Common mistakes
Mistakes. Alerting on free memory instead of available. Dropping caches to free RAM, which only slows the system down while the cache refills; we did it once, for the measurement. Judging apps by virtual size. Running without limits, so one leak takes down a whole node. Restarting OOM killed apps without looking for the leak. And setting a Java heap larger than the container limit.
- alerting on "free" memory
- dropping caches "to free RAM" (it only slows things)
- judging apps by VIRT
- no limits: one leak takes the whole node
- restarting OOM-killed apps without finding the leak
- JVM heap above the limit
Say it like this
In the interview: exit code one three seven means SIGKILL, and for a container it is usually the OOM killer hitting the memory limit. I confirm it in dmesg or with kubectl describe showing OOMKilled, then compare the limit with the process's real RSS over time to tell a leak from a limit that is simply too small. Then I fix the leak or set the limit from the measured peak plus headroom, with the runtime heap below it.
- "137 is SIGKILL, often the OOM
- killer. I check dmesg or kubectl describe for OOMKilled, compare the limit with real RSS
- over time, and look for a leak.
- Then set the limit from the measured peak plus headroom and the heap below it."