Virtual memory gives a process an address space that the operating system maps to physical memory with CPU support. Allocating memory reserves a range of virtual addresses; it does not necessarily allocate physical memory immediately. This distinction matters for latency because different accesses can require different amounts of work. section 7.4.3 section 7.4.4
The address space is divided into pages, typically 4 KiB or 2 MiB. A page table maps virtual addresses to physical memory at page granularity. Adjacent virtual pages can map to physical locations that are far apart, and a virtual page may have no physical counterpart until it is accessed. section 7.4.4
Contiguous virtual addresses, separate physical locations
The virtual range 0x0–0x1000 can map to physical addresses 0x100000–0x101000, while the adjacent range 0x1000–0x2000 maps to 0x200000–0x201000. The application sees adjacent virtual ranges even though their physical mappings are not adjacent. section 7.4.4
Figure 7.5 A page table is a data structure the CPU uses to translate virtual memory addresses to physical addresses. A page in virtual memory does not always have a counterpart in physical memory. Instead, the OS can store a page on the disk, for example, and lazily move it to physical memory when it’s accessed.
The page-table diagram shows address translation and a virtual page whose contents are stored on disk rather than in physical memory. section 7.4.4
With demand paging, physical pages are mapped lazily. When a thread first accesses an unmapped address, the CPU raises a page fault, transferring control to the operating system. Under memory pressure, the operating system can also swap a page to disk and read it back lazily when it is accessed again. Disk access can be at least 100 times slower than memory access, making these events important to latency-sensitive applications. section 7.4.4 section 7.4.3
Figure 7.4 A process’s virtual memory address space consists of sections that serve different purposes. The text, data, and BSS sections are initialized at program startup, whereas the heap, memory-mapped region, and stack sections change dynamically at runtime (denoted with dotted lines).
The address-space diagram distinguishes startup sections from the heap, memory-mapped region, and stack, which change at runtime. section 7.4.3
