Virtual Memory

paging

Suppose you must store many people's belongings in a shared warehouse. If you let each person reserve any oddly-shaped contiguous stretch, you soon get awkward gaps too small to reuse. The clean fix is to cut everything into identical boxes: chop each person's stuff into equal boxes and chop the warehouse into equal shelves, then any box fits any shelf. Paging is exactly this: memory is divided into fixed-size pieces so the mapping between a program's space and real memory becomes a simple, gap-free box-to-shelf assignment.

Concretely, the virtual address space is divided into fixed-size pages (commonly 4 KiB) and physical memory into equally-sized page frames. A program's page can be placed in any free frame, in any order — page 0 might sit in frame 90 while page 1 sits in frame 12. A page table records, for each virtual page, which frame holds it (or that it is not present). Because pages and frames are the same fixed size, no virtual page is ever 'too big' for a frame, and there is no external fragmentation: every free frame is usable by any page. The only waste is internal fragmentation — the leftover slack inside the last partly-used page.

Why it matters: paging is the foundation that makes virtual memory practical. Fixed-size units make allocation trivial (keep a list of free frames), make relocation free (just change which frame a page maps to), make protection clean (one set of permission bits per page), and make demand loading possible (bring in only the pages actually touched). The honest trade: small pages mean large page tables and more TLB pressure; large pages cut table size and TLB misses but waste more memory to internal fragmentation. Real systems offer multiple page sizes to balance this.

A 12 KiB program with 4 KiB pages has 3 pages. The OS may place them in frames 90, 12, and 51 — scattered and out of order — yet the program still sees one contiguous 0..12 KiB space because translation hides the scattering.

Scattered frames, contiguous illusion: paging decouples a program's view from physical placement.

Paging eliminates external fragmentation but introduces internal fragmentation. An older alternative, segmentation, used variable-size pieces and suffered the opposite problem; most modern systems page, sometimes layering simple segmentation on top.

Also called
paged memory分頁機制