NOTE
3.3 Virtual Memory
Virtual address spaces, page tables, demand paging, page faults, locality, swap, and why virtual memory is more than 'RAM plus disk'.
This is a historical learning note and may contain outdated or incomplete understanding.
1. What Virtual Memory Provides
Virtual memory gives each process a virtual address space that is translated to physical memory by hardware and the operating system.
The main benefits are broader than simply running programs larger than RAM:
- isolation between processes;
- flexible address-space layout;
- sparse mappings;
- shared mappings when explicitly requested;
- demand paging;
- file-backed memory mapping;
- protection bits such as read/write/execute.
Virtual memory should not be defined as “RAM + disk addressed as one large memory.” Disk or swap may back some pages, but the abstraction is fundamentally an address-translation and mapping system.
2. Paging
Virtual addresses are divided into pages. Page tables map virtual pages to physical page frames or record that a page is currently not resident.
A translation lookaside buffer (TLB) caches recent translations so every memory access does not require a full page-table walk.
3. Demand Paging and Page Faults
The OS does not need to populate every virtual page immediately.
When code accesses a page whose mapping is not currently resident or valid, the CPU raises a page fault. The kernel determines whether the access is legitimate and may:
- allocate a zero-filled page;
- load data from a mapped file;
- bring a swapped-out page back into RAM;
- perform copy-on-write;
- reject the access and terminate/signal the process.
4. Locality
Virtual-memory systems benefit from locality:
- temporal locality — recently accessed data is likely to be accessed again;
- spatial locality — nearby addresses are likely to be accessed soon.
This is why keeping an active working set in RAM can perform well even when the process’s total virtual address space is much larger.
5. When Memory Is Scarce
The OS may reclaim clean file-backed pages, write dirty pages, compress memory on some systems, or move anonymous pages to swap. Page-replacement policy determines which pages are better eviction candidates.
See Page Replacement.