Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / Operating Systems

    Virtual Memory: Every Process Gets Its Own Fake Addresses

    The booklet numbers are virtual addresses. Every process sees its own private range starting near

    Chapter 5 of 127 min read

    The Plain Picture#

    Every guest's room booklet says "your wardrobe is in room 101". Every guest's booklet says 101 — yet they don't share a wardrobe. Why? Because "101" is a room number inside each guest's own booklet. The front desk has a secret map: Guest A's 101 is really Floor 3, Room 12. Guest B's 101 is really Floor 7, Room 4.

    Guests never learn the real room numbers, so they can't walk into someone else's room even if they try.

    The Real Names#

    • The booklet numbers are virtual addresses. Every process sees its own private range starting near 0 (the layout from Ch 3).

    • The real positions in RAM are physical addresses.

    • The kernel's secret map for each process is its page table.

    • Memory is mapped in fixed-size chunks called pages — usually 4 KB on Linux servers, and 16 KB on Apple Silicon Macs (on your Mac, getconf PAGESIZE prints 16384).

    • A piece of CPU hardware, the MMU (Memory Management Unit), translates every virtual address to a physical one on every memory access, using the page table.

    This answers Part 0's questions 2 and 3: every process can use address 0x1000 because each one's 0x1000 maps to a different physical place, and a process has no way to even name another process's memory.

    Under the Hood#

    Translation#

       StudyBuddy                        Page table                    Physical RAM
       virtual address                   (StudyBuddy's)
      ┌──────────────┐                  ┌───────────────┐             ┌────────────┐
      │ page 0       │ ───────────────▶ │ 0 → frame 812 │ ──────────▶ │ frame 812  │
      │ page 1       │ ───────────────▶ │ 1 → frame 45  │ ──────────▶ │ frame 45   │
      │ page 2       │ ───────────────▶ │ 2 → on disk   │ ─ ─ ─ ─ ─▶  │ (not here) │
      └──────────────┘                  └───────────────┘             └────────────┘
                                                                       a "frame" = one
       Spotify's page 0 maps somewhere else entirely                   page-sized slot in RAM

    To keep this fast, the CPU caches recent translations in a small, fast table called the TLB (Translation Lookaside Buffer).

    Page faults — when the page isn't there#

    If a program touches a page that isn't currently in RAM, the MMU raises a page fault and the kernel steps in:

    • Page never used yet → the kernel finds a free frame, maps it, and continues. Cheap. (Memory is handed out lazily: a program that "allocates" 1 GB but never touches it uses almost no real RAM.)

    • Page was moved to disk → the kernel reads it back from disk. Very slow (How Computers Work Ch 6: disk is ~1,000× slower than RAM).

    • Address isn't valid for this process at all → the kernel kills it: segmentation fault.

    When RAM runs out: swap and the OOM killer#

    If processes need more memory than the machine has, the kernel can move less-used pages to a reserved area of the disk called swap. Programs keep running, but every touch of a swapped page means a disk read. When a machine spends most of its time moving pages in and out, it's thrashing — everything crawls.

    On Linux, if memory truly runs out, the kernel's OOM killer (Out Of Memory) picks a process — usually the biggest — and kills it to save the system.

    Why this matters for you: in Docker and Kubernetes (DevOps (coming soon)), each container gets a memory limit. If your FastAPI app goes over it, it is killed instantly. In Kubernetes this shows up as OOMKilled and exit code 137. It's one of the most common production incidents you'll debug.

    Shared pages#

    Page tables also make sharing cheap. If 10 processes use the same library, the kernel can map the same physical frames into all of them (read-only). And after fork (Ch 3), parent and child initially share all pages; a page is only copied when one of them writes to it. This trick is called copy-on-write, and it's why fork is faster than it sounds.

    Gotchas

    • "Memory usage" has several meanings. Virtual size (everything mapped, often huge and meaningless) vs resident size (RSS, what's actually in RAM right now). When debugging, look at RSS.

    • Free RAM is not wasted RAM on Linux. The kernel uses spare RAM to cache file contents (Ch 10), so a server showing "almost no free memory" is often perfectly healthy. Look at "available" memory instead.

    Check yourself

    5 questions
    1

    Why can two processes both use virtual address 0x1000 without conflict?

    Show answer

    Each process has its own page table, so the same virtual address maps to different physical memory.

    2

    What is a page, and how big is one on your Mac?

    Show answer

    A fixed-size chunk of memory used for mapping; 16 KB on Apple Silicon (4 KB is typical on Linux).

    3

    What are the three kinds of page fault outcomes?

    Show answer

    Map a fresh page (cheap), read a swapped page back from disk (slow), or kill the process for an invalid address (segmentation fault).

    4

    What does the OOM killer do, and how does it show up in Kubernetes?

    Show answer

    Kills a process when memory runs out; in Kubernetes the container shows OOMKilled, exit code 137.

    5

    What is copy-on-write?

    Show answer

    After a fork, pages are shared until one side writes, and only then is that page copied.