Skip to content

Lesson 5 of 9 · 25m

Memory Pressure: Cache, Reclaim, Swap, and OOM

Why free memory is the wrong metric, how reclaim and PSI expose pressure, and how to prevent mystery OOM kills.

Linux memory reporting confuses everyone once. The system says only 300 MB is free on a 64 GB host, someone panics, and then the actual incident turns out to be a cgroup limit or pathological reclaim. The kernel intentionally uses spare memory for page cache. Free memory is wasted memory; memory pressure is something else entirely.

Read free correctly

free -m

The available column is the useful estimate: memory applications can allocate without forcing heavy reclaim. buff/cache is mostly page cache the kernel can drop when needed. But "can drop" is not free either — cache often exists because rereading it from disk would be expensive. The goal is not maximum free memory; it is enough headroom that allocation does not stall critical work.

Detect real pressure

cat /proc/pressure/memory
vmstat 1 5

In vmstat, watch si and so for swap-in and swap-out. Occasional swap-out on a mostly idle service may be harmless; sustained swap-in during request processing is a latency disaster. PSI is sharper:

some avg10=12.44 avg60=8.90 avg300=3.51 total=4829102
full avg10=4.01 avg60=2.20 avg300=0.74 total=1182841

some means at least one task stalled on memory; full means every non-kernel task stalled. Rising full stall time is a strong sign that allocation is directly hurting throughput.

Anonymous memory, page cache, and slab

Memory is not one bucket. Anonymous memory is process heap and stack. Page cache backs file data. Slab is kernel object memory — dentries, inodes, sockets, and more. Use smem, /proc/meminfo, and slabtop when totals do not line up.

grep -E 'MemTotal|MemAvailable|Active|Inactive|Slab|SReclaimable' /proc/meminfo
sudo slabtop -s c | head

A classic failure mode is a backup job reading hundreds of gigabytes through page cache and pushing out hot application cache. The application did not leak, but latency rises because every file read now goes back to disk. Use vmtouch or application-level cache metrics when cache warmth matters.

OOM kills are usually policy, not surprise

dmesg -T | grep -i -E 'oom|killed process'
journalctl -k --since '1 hour ago' | grep -i oom

The kernel OOM killer chooses a victim under pressure, but in containerized systems the more common killer is a cgroup memory limit. Read memory.events inside the cgroup:

cat /sys/fs/cgroup/memory/memory.events 2>/dev/null
cat /sys/fs/cgroup/memory.current /sys/fs/cgroup/memory.max 2>/dev/null

If oom_kill increments while host memory looks fine, the container hit its own ceiling. Raising swap or kernel memory tuning will not fix a hard cgroup limit.

Field notes

  • Do not disable swap just because swap is "bad"; a little swap can absorb cold memory while PSI tells you whether it hurts.
  • echo 3 > /proc/sys/vm/drop_caches is not tuning. It destroys useful cache and hides evidence.
  • Transparent hugepages can cause allocation stalls for some databases; measure before disabling globally.
  • Memory leaks are growth with no plateau under stable load. One upward graph is not proof until workload and cache effects are ruled out.

Was this lesson helpful?

Sign in to rate