Operating Systems

Virtual Memory

Each process sees its own address space, backed by RAM and disk as needed.

Level: intermediate

What is it?

Virtual memory is the illusion that every process owns a large, private, contiguous address space. The hardware translates VA → PA through page tables and a TLB. The OS fills the illusion: demand paging, copy-on-write, file-backed maps, swap.

Why does it matter?

Isolation, convenient linking (every process can start at the same VA), and overcommit. It is also why 'out of memory' and 'page fault' are different sentences.

Mental model

A library card catalog. Your card names a shelf that might be in the reading room (RAM) or in off-site storage (disk). The clerk (page fault handler) fetches as needed.

How it works

  • Translation is the always-on part. Swap is optional.
  • Demand paging: a freshly mmap'd file is not all in RAM.
  • COW fork: parent and child share frames marked read-only; a write faults and copies.
  • The working set must fit or you thrash.

Real-world usage

  • Every user process
  • mmap
  • Shared libraries

Common mistakes

  • Equating virtual memory with swap
  • Thinking a page fault is always a bug

Interview questions

Is swap required for virtual memory?

No. Translation and isolation work without swap. Swap is a way to overcommit RAM.

What is copy-on-write fork?

Parent and child share physical pages marked read-only. A write triggers a fault; the OS copies the page and resumes.

Prerequisites

Related concepts

Continue learning

Browse the full computer science library