map / systems / virtual-memory

Systems · principle 3 of 7

Virtual memory

Every process sees its own address space mapped onto physical pages on demand, which is why memory “used” and memory “resident” differ.

01 · Why it’s true

Each process sees its own private address space. The CPU translates every virtual address to a physical page using page tables the kernel maintains, so processes can’t see each other’s memory.

Physical memory is only assigned when a page is first touched. That’s why a process’s virtual size (VSZ) is often much larger than its resident memory (RSS). When RAM runs short, the kernel first reclaims page cache, may swap, and — as a last resort, or when a container hits its memory limit — the OOM killer ends a process.

02 · Picture it

1Virtual addresswhat the program useslookup2Page table (MMU)translates every accessmapped3Physical pagereal RAM, assigned onfirst touchNot mapped yet → page fault → the kernel finds a free page, reclaiming cache if needed.Nothing left to reclaim, or a cgroup limit hit → the OOM killer ends a process.
1Virtual addresswhat the program useslookup2Page table (MMU)translates every accessmapped3Physical pagereal RAM, assigned on first touchNot mapped yet → page fault → thekernel finds a free page, reclaimingcache if needed. Nothing left toreclaim, or a cgroup limit hit → theOOM killer ends a process.
Every memory access is translated; physical RAM is handed out lazily.

03 · In the interview

  • “Why is free memory nearly zero?” — page cache uses spare RAM and gives it back when needed.
  • VSZ versus RSS, and which one to watch.
  • Diagnosing OOM kills in containers.

04 · Practice question

“A container was OOM-killed, but the application’s own memory metrics were well under the limit. How?”

A strong answer covers

  1. Confirm the kernel killed it: exit code 137 and an out-of-memory message in the kernel log.
  2. The container’s limit counts everything charged to it — not just the heap the app reports: thread stacks, native buffers, other processes, tmpfs files.
  3. Compare RSS and the cgroup’s memory statistics with what the app reports to find the gap.
  4. Look at growth over time (leak) versus spikes with load, then fix the cause or resize the limit with headroom.

Want to play with it? Open the interactive deep dive →