Virtual Memory

Virtual memory applies the same hierarchy idea one level down: main memory acts as a cache for the disk. It lets each program believe it has a huge private address space, while the OS maps it onto whatever physical RAM is actually free.

1. Why virtual memory

  • Bigger-than-RAM programs — inactive parts live on disk, pulled in on demand.
  • Isolation & protection — each process has its own address space; it can't read another's memory.
  • Relocation — a program's virtual addresses can map to any physical frames, so it doesn't care where it's loaded.

2. Pages, frames & the address split

The virtual address space is divided into fixed-size pages; physical memory into equal-size frames. A virtual address splits into a virtual page number (VPN) and a page offset:

Virtual page numberwhich page4 bits0 – 3Page offsetbyte within page12 bits4 – 15
Slide example: 16-bit virtual address, 4 KB pages → offset = log₂(4096) = 12 bits, VPN = 16 − 12 = 4 bits ⇒ 16 virtual pages.
Formulas. page offset bits = log2(page size)\log_2(\text{page size}) · VPN bits = addr bitsoffset bits\text{addr bits} - \text{offset bits} · number of virtual pages = 2VPN bits2^{\text{VPN bits}}.
The page offset is unchanged by translation — only the page number is mapped.

3. The page table translates VPN → frame

The page table has one entry (PTE) per virtual page. Indexing it with the VPN gives the physical frame number (plus a valid bit). Concatenating that frame number with the untouched offset yields the physical address.

Virtual address
VPN + page offset
Index the page table with the VPN
page table base + VPN × entry size
Valid bit = 1?
if 0 → PAGE FAULT: OS fetches the page from disk
Physical address = frame number ∥ page offset
Address translation. A page fault is the virtual-memory equivalent of a cache miss — but the penalty (disk) is enormous.

4. Vocabulary map: cache ↔ virtual memory

Cache termVirtual-memory term
Block / linePage
Cache missPage fault
Miss penaltyDisk access (millions of cycles)
Tag + index lookupPage-table lookup (VPN → frame)
Because page faults are so costly, virtual memory favours large pages, fully-associative placement (any page in any frame), and clever replacement — the opposite balance from a small fast cache. A TLB (a tiny cache of recent translations) hides the page-table lookup cost.
Section checklist
  • Main memory caches the disk; pages ↔ frames.
  • Split a virtual address: offset = log₂(page size), VPN = the rest.
  • Page table maps VPN → frame; a missing page = page fault.