Skip to content

Lesson 4 of 9 · 25m

Virtual Memory and Paging: The Contract Behind Every Address

Pages, page faults, RSS versus virtual size, overcommit, and why memory pressure is behavior rather than a single number.

Every pointer your program uses is a lie — a useful lie. The address is virtual, translated by the CPU and kernel into physical memory one page at a time. Once you understand that contract, half of Linux memory confusion evaporates: huge virtual sizes are normal, RSS is incomplete, free memory is not the metric, and page faults explain latency spikes.

Virtual addresses buy isolation and flexibility

A process sees a private address space laid out in regions:

$ cat /proc/self/maps | head
555555554000-555555558000 r-xp 00000000 ... /usr/bin/cat
555555757000-555555758000 r--p 00002000 ... /usr/bin/cat
555555758000-555555759000 rw-p 00003000 ... /usr/bin/cat

Each mapping has permissions and a backing object: executable file, heap, stack, shared library, anonymous memory, or memory-mapped file. The kernel does not need contiguous physical memory, and two processes can share read-only file pages. That is why ten copies of the same binary do not consume ten copies of its text segment.

Page faults are normal — some are expensive

A page fault means the CPU touched a virtual page that is not currently usable. Minor faults are cheap bookkeeping: first touch of heap memory, copy-on-write after fork, or mapping part of a file. Major faults require disk I/O and can stall the thread for milliseconds.

ps -o pid,min_flt,maj_flt,rss,cmd -p 3211

If maj_flt rises steadily during latency spikes, memory pressure or mmap-heavy access patterns are pushing work to disk. Minor faults rising is usually just allocation churn.

RSS is not 'memory used'

RSS counts resident pages attributed to the process, but shared pages are counted in each process that maps them. Sum RSS across a fleet of Python or Java processes and you overestimate shared-library memory. Conversely, RSS excludes clean pages already evicted but still part of the process's working set.

Better views:

cat /proc/meminfo
cat /proc/pressure/memory
smem -k | head

MemAvailable is the kernel's estimate of memory available without dangerous reclaim. PSI memory pressure tells you when tasks stalled because memory had to be reclaimed.

Overcommit and the OOM killer

Linux often permits allocating more virtual memory than physical memory because many allocations are never touched. When actual demand exceeds reclaimable memory plus swap, the kernel chooses a victim and the OOM killer terminates it. That decision is based on badness heuristics, not business priority.

The signature:

dmesg -T | grep -i -E 'out of memory|oom-kill'

You will see which process was killed, its RSS, and memory statistics at the time. The fix is not always 'add RAM.' It may be bounding caches, correcting a leak, lowering concurrency, or setting container memory limits so failure is isolated rather than machine-wide.

Swapping is not automatically evil

Swap can absorb cold anonymous memory and keep hot workloads stable. The problem is swap-in latency: a process touching a swapped-out page stalls on storage. If swap activity is constant and hot pages cycle in and out, you are beyond useful swap and into thrashing. vmstat's si/so columns show swap-in and swap-out rates; persistent non-zero rates plus latency complaints are a strong signal.

Field notes

  • 'Free memory low' often means page cache is doing its job; look at MemAvailable and reclaim pressure. Transparent huge pages can also improve or hurt latency depending on workload.
  • Memory leaks are growth curves, not single high readings. Plot RSS or heap metrics over hours before calling it a leak.
  • Copy-on-write makes fork cheap initially, then expensive when parent and child write heavily. Pre-fork servers with large mutable heaps need care.

Was this lesson helpful?

Sign in to rate