Virtual memory is the address space an operating system presents to a process using CPU support. Allocating memory reserves a range of virtual addresses, not necessarily physical memory at that moment. The address space is divided into pages, typically 4 KiB, though larger pages are possible. section 7.4.3
A page table maps virtual pages to physical pages. Adjacent virtual pages need not occupy adjacent locations in physical memory: the 4 KiB range from 0x0 to 0x1000 could map near physical address 0x100000, while the next range could map near 0x200000. The process still sees a contiguous virtual range. section 7.4.4
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 process address-space diagram separates startup sections—text, data, and BSS—from the heap, memory-mapped region, and stack, which change at runtime. section 7.4.3
Physical pages can be assigned lazily. On a first access, the CPU raises a page fault and transfers control to the OS, which can allocate a page. Under memory pressure, the OS may instead have stored a page on disk and read it back only when accessed again. This makes access latency variable: disk access is at least 100 times slower than memory access, so paging can matter in latency-sensitive applications. section 7.4.3 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 makes the key distinction visible: a virtual page does not always have a physical-memory counterpart; its contents may be on disk until accessed. section 7.4.4
