interviewpath
  • 0day streak
  • 10total XP
  • Level 1
0/6

Unit 3 · OS & concurrency · Lesson 2 of 11

Virtual memory and memory management

Virtual memory gives every process a private address space and lets the OS map, share, lazily allocate and reclaim physical memory. It explains page faults, copy-on-write, confusing memory-usage numbers and out-of-memory kills, which come up whenever a service is slow or killed for memory.
About 5 hFocus mode

Step 1: Learn the ideas

  • Address spaces and isolation
  • Paging, page-table entries and permission bits
  • Multi-level page tables
  • TLBs, address-space IDs and TLB shootdowns
  • Page faults: minor, major and invalid
  • Demand paging, demand-zero pages and overcommit
  • Copy-on-write and fork
  • Memory-mapped files and the page cache
  • Swapping, replacement policies and thrashing
  • Allocators and fragmentation (internal and external)
  • Huge pages
  • Memory accounting: virtual size, RSS, PSS and OOM kills

Why it matters

Virtual memory is behind isolation between processes, cheap process creation, memory-mapped files and the gap between "allocated" and "actually used" memory. In practice it appears as questions like: why does a process show 30 GB of virtual memory but use 2 GB, why was the container OOM-killed when its heap was small, why does a forked snapshot double memory under write load, and why did huge pages speed one workload up and add latency spikes to another. Interviewers check that you can move between the textbook mechanism and these production symptoms.

Core ideas

Translation. A virtual address splits into a page number and an offset. The page table maps page numbers to physical frames plus permission bits (present, writable, user, executable) and status bits (accessed, dirty). With 4 KiB pages and 9-bit indexes per level, x86-64 walks four levels (five with the larger address-space extension) and RISC-V Sv39 walks three. Multi-level tables allocate lower levels only for regions in use, which is what makes huge, sparse address spaces affordable.

The TLB. Each translation could cost several memory accesses, so the CPU caches recent ones in the TLB. TLB reach, entries times page size, bounds how much memory you can touch without misses; 2 MiB and 1 GiB pages multiply it. Address-space identifiers (PCID on x86, ASIDs elsewhere) avoid flushing on every process switch. When the kernel unmaps or write-protects a page that other cores may have cached, it must interrupt them to invalidate their entries, a TLB shootdown, which is one reason munmap and mprotect are not free in multithreaded programs.

Page faults are normal. A fault means the hardware could not complete a translation, and the kernel decides what it means. A minor fault needs no I/O: first touch of a fresh anonymous page, a file page already in the page cache, a copy-on-write copy. A major fault must read from storage, from swap or a file. An access outside any valid mapping becomes a segmentation fault. Linux counts both kinds per process, visible for example through /usr/bin/time -v.

Lazy allocation and overcommit. A large malloc usually just maps address space; physical frames are assigned on first touch. Linux overcommits by default within a heuristic limit, so running out of memory often shows up later as the OOM killer choosing a victim rather than as an allocation returning NULL.

Copy-on-write. After fork(), parent and child share frames marked read-only. The first write to such a page faults; the kernel copies it and maps the copy writable. Frames now have several owners, so the kernel reference-counts them and frees a frame only when its last mapping disappears. A fork-based snapshot stays cheap only while few pages are written.

File-backed versus anonymous memory. The page cache and memory-mapped files hold pages that can be dropped if clean or written back if dirty. Anonymous memory (heap, stacks) can only be reclaimed by swapping; without swap, sustained pressure ends in OOM kills. Replacement policies approximate LRU with accessed bits, as in the clock algorithm, and when the working set exceeds memory the system thrashes.

Allocation and fragmentation. Internal fragmentation is waste inside an allocated block, from rounding up to a size class or a page. External fragmentation is free memory split into pieces too small for a request. Paging removes external fragmentation for ordinary page-sized mappings, but it returns for huge pages and physically contiguous buffers, which is why the kernel compacts memory. User-space allocators use size classes and per-thread caches to stay fast under concurrency, accepting some internal fragmentation and retained memory.

Common misconceptions

  • "Virtual memory means swapping to disk." Its main jobs are translation and isolation; many servers run without swap.
  • "If malloc succeeded, the memory is there." With lazy allocation and overcommit, frames are committed on touch, and failure can surface later as an OOM kill.
  • "A page fault means a disk read." Most faults are minor and part of normal operation; only major faults wait for storage.
  • "RSS is the memory my process uses." RSS counts shared pages in every process that maps them and excludes page cache the process caused; proportional set size and cgroup accounting answer different questions.
  • "Huge pages are always faster." They raise TLB reach but can add internal fragmentation, compaction stalls and expensive copy-on-write of large pages, which is why some latency-sensitive services disable transparent huge pages.
  • "Paging eliminates fragmentation." Only external fragmentation for page-granular mappings; internal fragmentation and contiguous-allocation failures remain.

How to approach it

Do one translation by hand for x86-64 or Sv39, splitting the address into indexes and offset and walking the levels, and compute how much page-table memory a small sparse process needs. Then make faults visible: touch a large array once and compare minor-fault counts, and fork a process with a big heap and write to it while watching memory grow. Graduate interviews focus on translation, TLBs and faults; mid-level ones on copy-on-write, overcommit and reading memory metrics; senior and infrastructure interviews on reclaim, OOM behavior under container limits, NUMA and huge pages.

Suggested order

  1. 1Read OSTEP Ch. 13–15 quickly for address spaces, the memory API and base-and-bounds translation.
  2. 2Read OSTEP Ch. 18–20 (paging, TLBs, smaller tables) and translate a few addresses by hand.
  3. 3Read OSTEP Ch. 21–22 for page faults, swapping, replacement policies and thrashing, and Ch. 17 (Free-Space Management) for fragmentation.
  4. 4Read OSTEP Ch. 23 (Complete Virtual Memory Systems) and the xv6 book Ch. 5 (Page faults) for copy-on-write, lazy allocation and memory-mapped files in real systems.
  5. 5Skim the Linux memory-management concepts overview and its overcommit page to connect the theory to OOM kills and huge pages.
  6. 6Do the OSTEP TLB measurement homework, then the 6.1810 page tables and copy-on-write labs.

Step 2: Check off what you can do

Tap each one you can genuinely do or explain. Each earns 10 XP.

Step 3: Go deeper

  • Operating Systems: Three Easy PiecesStart here

    University of Wisconsin–Madison (free online book)

    Section: Ch. 19: Paging: Faster Translations (TLBs)

    Core reading on TLB behavior, reach and context switches; read Ch. 18 and 20 around it for paging and multi-level tables.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • Operating Systems: Three Easy Pieces

    University of Wisconsin–Madison (free online book)

    Section: Ch. 21: Beyond Physical Memory: Mechanisms

    Page faults, the present bit and swap; continue with Ch. 22 for replacement policies and thrashing.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • Operating Systems: Three Easy Pieces

    University of Wisconsin–Madison (free online book)

    Section: Ch. 23: Complete Virtual Memory Systems

    How VAX/VMS and Linux combine the mechanisms: demand zeroing, copy-on-write, large pages and the page cache.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • MIT 6.1810 Operating System Engineering (xv6)Start here

    MIT PDOS

    Section: xv6 book, Ch. 5: Page faults

    Lazy allocation, copy-on-write fork, demand paging and memory-mapped files explained against real kernel code; prepares the COW lab.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • Linux memory management: Concepts overview (kernel documentation)

    The Linux Kernel documentation (docs.kernel.org)

    Linux vocabulary for zones, nodes, page cache, anonymous memory, reclaim and the OOM killer; follow its links to overcommit accounting and transparent huge pages.

    Open(opens in a new tab)Link responded, 5 Oct 2026

Step 4: Practice

Begin with the TLB measurement homework on Linux: plot time per access against pages touched and explain each knee in the curve. Then do the page tables and copy-on-write labs; in the COW lab, write down who owns each physical page at every step. On a toy program, use /usr/bin/time -v and /proc/<pid>/smaps to watch minor and major faults and RSS change as it touches memory and forks. LRU Cache is a coding-round bridge: after solving it, explain why kernels approximate LRU with accessed bits instead of keeping an exact list.

Record attempts on each problem’s page (+15 XP, +10 more when solved on your own).

Step 5: Test yourself

Answer each one out loud or on paper before you finish the lesson.

  1. Question 1Why does a multi-level page table use less memory than a linear one for a typical process?
  2. Question 2What is TLB reach, and how do huge pages change it?
  3. Question 3Why can writes after fork() be slow even though fork() itself returned quickly?
  4. Question 4How can a process be OOM-killed even though its last malloc() succeeded?
  5. Question 5Why must the kernel interrupt other cores when it unmaps a page?
  6. Question 6What is the difference between internal and external fragmentation? Give one example of each.
  7. Question 7Which pages can the kernel reclaim without swap, and which cannot be reclaimed?

You’re done when you can

  • Translate a virtual address through a three- or four-level page table by hand without errors.
  • Explain copy-on-write fork, including reference counting of shared frames, in under three minutes.
  • Given a process with a large virtual size and a moderate RSS, explain the difference and name the other numbers you would check.
  • Classify five described faults as minor, major or invalid and justify each answer.
  • Complete the copy-on-write lab or an equivalent exercise that handles a write fault correctly.

Finish the lesson

How confident do you feel?

Lesson complete!

+50 XP

Reviews will come up on the Practice tab so it sticks.

Unlocks next: CPU caches, locality, coherence and NUMA, Files, page cache, storage and persistence, Containers, VMs, namespaces and cgroups

Your notes and study time

Log study time