liveBook · Manning

Page fault

As explained in Latency

A page fault is a processor-generated exception raised when an accessed virtual memory page is not currently mapped to usable physical memory.

A page fault is a processor-generated exception raised when a thread accesses a virtual-memory page that is not currently mapped to usable physical memory. The hardware trap transfers control to the operating system, which can allocate a physical page, load contents from a mapped file, or restore an evicted page from swap. section 7.4.4

Demand paging creates mappings lazily. Process startup establishes text, data, BSS, heap, and stack regions without immediately assigning physical pages; first access supplies the trigger. This saves work and memory initially, but makes access latency unpredictable because fault handling may require disk I/O. Memory pressure can later evict anonymous pages to swap and mark them non-present, causing another fault when they are accessed. section 7.4.4

Page faults matter in several common memory-management paths:

  • Map a file with mmap(); the operating system pages file data into memory as the application accesses it, avoiding explicit read() and write() calls. section 10.2.5
  • Expect readahead to hide some I/O latency, but recognize that unsuitable access patterns or a working set larger than memory can amplify latency. section 10.2.5
  • Protect latency-sensitive regions by preallocating virtual memory and pinning pages with mlock() on Linux or VirtualLock() on Windows, reducing eviction-driven faults. section 7.4.4

First access to a virtual page

Suppose a process first touches a virtual page corresponding to an address such as 0x200000. Although the process sees contiguous virtual addresses, the operating system may assign a physical page at an unrelated location, such as the range 0x200000–0x201000 in physical memory. The first access faults, the operating system establishes the mapping, and later accesses can use it unless the page is evicted. 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.

Use the page-table figure to trace how a virtual page can lack a physical counterpart, remain on disk, and move into memory only after access. section 7.4.4

How Latency covers it

How deep it goes
Traces the hardware trap, operating-system response, file loading, swapping, and latency trade-offs, but not handler implementation.
Examples
Uses executable sections, a 0x200000 address example, mmap(), file-backed pages, swap, and pinned memory.
What it assumes
Assumes virtual and physical memory terminology while explaining lazy mappings and fault handling directly.
What you can do after
Evaluate page-fault latency and choose memory mapping, pinning, or access-pattern strategies for workload constraints.
How it connects
Connects page faults to virtual memory, demand paging, page tables, memory mapping, swapping, readahead, and latency.

Also explained in

Compare how 2 books explain page fault →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Latency. The section links point to the parts of the book it draws on.