Skip to content

Lesson 8 of 13 · 14m

What Memory Is Actually Used

`available` beats `free`, `PSS` beats `RSS`, and a leak is growth that never plateaus.

free -h reports a free column, and every week someone files "the server is out of memory" because that column is small. The page cache is not usage — it is the kernel holding recently-read file data that any application can reclaim on demand. The column that means something is available: the kernel's estimate of memory a new process could allocate without swapping, which counts most of cache as usable. "Linux ate my RAM" is a meme precisely because this misreading is universal.

The per-process version of the same lie

RSS counts shared pages fully in every process that maps them — sum RSS across a worker pool and you double-count libc, mmap'd files, and shared memory segments. VSZ is worse: virtual size counts reservations, not resident pages, and a process can carry terabytes of VSZ on a gigabyte host. The honest number is PSS in /proc/<pid>/smaps_rollup: shared pages divided among their sharers. A worker showing 400 MB RSS that shares 300 MB of it with its siblings costs you roughly 100 MB.

Where memory actually leaks

Language runtimes allocate from their own allocators, so the leak is usually in a cache, not in malloc. Unbounded maps, growing buffers, per-request structures retained by a closure — the process graph climbs linearly for days. That slope is also how you distinguish a leak from a warmup plateau: leaks do not level off.

For C/C++, heaptrack records every allocation with its stack; valgrind --tool=massif gives heap-over-time; both show which call tree holds the growth. jemalloc has built-in profiling (MALLOC_CONF=prof:true,prof_prefix:/tmp/jem) consumable by jeprof. Go's runtime/pprof heap endpoint (/debug/pprof/heap) reports alloc_space versus inuse_space — in-use is the leak signal.

The glibc arena gotcha

glibc malloc keeps per-thread arenas — by default up to eight times the core count on 64-bit — specifically to keep threads off one central lock. Under fragmentation, a 64-core box grows dozens of arenas that each hold freed-but-unreturned memory, and RSS climbs while the real heap stays flat. M_ARENA_MAX caps the arena count; jemalloc or tcmalloc replaces the allocator outright and usually returns memory better. The symptom pattern: RSS scales with thread count while heaptrack and pprof show no leak. That is arena bloat, not a leak.

Swap is not automatically failure

Swap usage alone is not an emergency — the kernel swaps cold pages it is confident are idle (vm.swappiness biases the choice), and a box with 2 GB in swap and zero swap I/O is fine. The failure mode is swap activity: vmstat's si/so columns doing sustained work while iostat shows the swap device hot. Memory pressure announces itself in /proc/pressure/memory long before the OOM killer fires — the some/full stall averages tell you threads are already waiting on reclaim. When you need PSS without parsing smaps by hand, smem -k and ps_mem print it per process.

Evidence

OOM kills are the evidence people misread most. The kernel picks a victim by oom_score, a heuristic over footprint and niceness, and it often kills the biggest victim, not the leaking cause. After a kill, journalctl -k holds the oom-killer's dump — per-task RSS at kill time and the chosen victim's name. Read it before assuming the named process was the problem.

The practice

  • Read available, not free; per process, read PSS in smaps_rollup, not RSS.
  • Leak = growth that never plateaus; confirm with the allocator's own view (heaptrack, massif, pprof) before blaming the kernel.
  • On multithreaded glibc bloat, try M_ARENA_MAX or jemalloc before hunting phantom leaks.
  • After an OOM kill, read the kernel dump — the named victim is not automatically the culprit.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.