Taking pages back under pressure: what's reclaimable (clean file first, dirty writeback, anon needs swap, slab shrinkers), the active/inactive LRU lists & second-chance/refault, watermarks with kswapd vs direct reclaim, swappiness & thrashing, and the OOM killer (incl. cgroup OOM).
When memory runs low, the kernel has to take pages back — deciding what to evict, writing out what's dirty, and doing it without stalling everyone. How reclaim works, and why "high memory use" is usually fine. Original, interview-focused notes; the pressure side of Memory Management.
Reclaim frees the easy pages first: clean file-cache pages can be dropped instantly (they're on disk already); dirty pages must be written back first; anonymous pages need swap. It runs in the background (kswapd) and, when that's not fast enough, synchronously in the allocating task (direct reclaim).
| Page type | To reclaim |
|---|---|
| Clean file (page cache) | Just drop it — the cheapest, reclaimed first. |
| Dirty file | Write back to disk, then drop. |
| Anonymous (heap/stack) | Write to swap, then drop. No swap → can't reclaim → OOM. |
| Reclaimable slab | Freed via shrinkers (dentry/inode caches). |
| Locked / kernel / mlock'd | Not reclaimable. |
vm.swappiness (0–100, 200 on newer kernels) biases the balance between reclaiming
file cache vs swapping anonymous pages. Low = prefer dropping file cache; it does not
disable swap.When reclaim can't free enough and there's no swap headroom, the OOM killer picks a victim by
oom_score (roughly memory footprint, tunable with oom_score_adj) and kills it — logged in
dmesg. Per-cgroup limits (memory.max) trigger a cgroup OOM scoped to that
group, which is how containers get killed. See the
troubleshooting angle.
swappiness actually tune, and does 0 disable swap? (file vs anon balance; no)