DEVOPS, SRE AND CLOUD · PART 1 · LINUX, FROM THE INSIDE · INTERNALS · 19 OF 187

Linux Memory: Page Cache, Swap and the OOM Killer

How Linux uses memory, measured: virtual memory and RSS (ShopRung used 47 MB resident), why "free" looks low (reading a 500 MB file took 309 ms cold and 76 ms from the page cache, which grew by about 512 MB), what "available" really means, swap, cgroup memory limits, and the OOM killer: our leaking program was killed at a 100 MB limit with exit code 137.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
1 / 11

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • RSS not VIRT
  • page cache: 309 ms → 76 ms
  • watch "available"
  • swap
  • cgroup limits
  • OOM kill = 137

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.

Two numbers per process
  • 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.

Our test: a 500 MB file
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.

free -m (our machine)
        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.

Swap
  • 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.

When memory runs out
  • 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.

Our run
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.

Same mechanism
  • 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.

Avoid these
  • 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.

In the interview
  • "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."

Interview questions

Q1What is the difference between VSZ and RSS?

VSZ (virtual size) is the address space a process has reserved; RSS (resident set size) is the memory actually in RAM. Use RSS to judge real usage.

Q2Why does free show so little free memory?

Linux uses spare RAM for the page cache; it is reclaimed instantly when needed. Look at the "available" column instead.

Q3What is the OOM killer?

The kernel mechanism that, when memory cannot be freed, picks a process (by oom_score) and kills it with SIGKILL; it is logged in dmesg / journalctl -k.

Q4What does OOMKilled mean in Kubernetes?

The container exceeded its memory limit (a cgroup limit) and was killed by the kernel, exit code 137.

Q5How do you tell a memory leak from a limit that is too small?

Graph RSS over time: a leak grows steadily without levelling off; a too-small limit is hit at a stable peak under normal load.

Q6Should you drop caches to free memory?

No. Page cache is reclaimed automatically; dropping it just makes the next reads slower.

Q7What is swap and why do many servers disable it?

Disk space used as overflow memory; heavy swapping makes systems very slow, so many cloud and Kubernetes setups prefer to fail fast and rely on limits instead.

Q8How do you protect an important process from the OOM killer?

Lower its oom_score_adj (for example OOMScoreAdjust= in systemd), and give other workloads memory limits.