Swapping moves inactive memory pages between RAM and disk when a program needs memory that is not currently in physical RAM. A page is a fixed-size block of memory, typically 4 KB on 64-bit operating systems. When a valid address refers to a page outside physical RAM, the CPU raises a page fault so the operating system can bring that page back. section 6.4.1
Virtual memory lets programs see a complete address space even when the machine cannot hold all their data in RAM at once. The operating system can overallocate memory, temporarily moving inactive pages to disk and restoring them byte for byte when the active program requests them. section 6.4.3
Swapping works within a larger virtual-memory system:
- Virtual addresses give programs their view of memory, while real memory describes the physical memory managed by the operating system. section 6.4.1
- Page tables translate virtual addresses to physical locations, and the CPU’s MMU helps manage that translation. section 6.4.1
- High memory contention commonly triggers swapping, although operating systems may also use it in other circumstances. section 6.4.3
An overallocated program
A program requests more memory than the machine can physically provide. The operating system keeps active pages in RAM and swaps inactive pages to disk. If the program later accesses one of those pages, the resulting page fault signals that the page must be brought back into physical memory. section 6.4.1 section 6.4.3
