Page fault

On this page 8

A page fault is a processor exception raised by the memory management unit when a program accesses a virtual address whose page is not present in physical memory or is mapped with permissions that forbid the access. In memory management, the page fault is not an error by itself: it is the mechanism that makes virtual memory work, letting the operating system load pages on demand, share memory and enforce protection.

Most page faults are resolved invisibly in microseconds. Only faults the kernel cannot satisfy turn into a crash, and frequent faults that require disk access are a leading cause of slow systems.

How the processor and kernel handle a page fault

A page fault begins when the memory management unit translates a virtual address and finds that the page table entry is marked not present or lacks the required permission. The CPU stops the faulting instruction, records the address and the reason, and transfers control to the kernel’s fault handler.

  1. The MMU misses in the translation lookaside buffer and walks the page table.
  2. The entry is invalid or forbids the access, so the CPU raises the exception; on x86 this is vector 14, with the address in the CR2 register.
  3. The kernel looks up the virtual memory area that contains the address and checks whether the access is legal.
  4. If legal, the kernel finds or allocates a page frame, fills it with the right content and updates the page table entry.
  5. The kernel returns from the exception and the CPU restarts the faulting instruction, which now succeeds.

The process never notices the interruption except as added latency. Because the instruction is re-executed from the start, processors must be able to undo partial effects, which is one reason page fault support is designed deeply into the instruction set architecture.

Minor, major and invalid page faults

Page faults fall into three practical classes that differ by where the data comes from and how long the fix takes. Operating systems report them separately, and the distinction is central to performance diagnosis.

ClassCauseData sourceTypical cost
Minor (soft) faultPage already in RAM but not mapped for this processPage cache or shared frameAbout 0.1 to a few microseconds
Major (hard) faultPage must be read from storageSwap area or file on diskAbout 50 to 200 microseconds on NVMe; several milliseconds on HDD
Invalid faultAddress unmapped or access forbiddenNoneSignal or exception to the process
Classes of page faults

Minor faults are extremely common: a process that maps a shared library typically takes thousands of them during startup. Major faults are the expensive ones, because a single one costs as much time as tens of thousands of minor faults on a hard disk. An invalid fault becomes a segmentation fault on Unix-like systems or an access violation on Windows.

Why operating systems cause page faults deliberately

Operating systems rely on page faults to postpone work until it is actually needed. Instead of copying or loading memory in advance, the kernel marks pages as absent or read-only and lets the first real access trigger a fault.

The following techniques all depend on intentional page faults.

TechniqueWhat the fault triggersBenefit
Demand pagingLoading a page from the executable or fileFast program start, lower RAM use
Copy-on-writeCopying a shared page on first writeCheap process creation with fork
Zero-fill on demandAllocating a zeroed frame on first touchLarge allocations cost nothing until used
SwappingReading an evicted page back from swapMore virtual memory than physical RAM
Memory-mapped fileReading file content into the page cacheFile access through ordinary loads and stores
Guard pagesDetecting stack overflow or buffer overrunEarly, reliable crash instead of corruption
Memory management features built on page faults

Garbage collectors, database engines and userfaultfd-based live migration also intercept page faults on purpose to track which memory a program touches.

A page fault is often confused with neighboring events in the memory hierarchy, yet each occurs at a different level and costs a different amount. The comparison below separates them.

EventLevelHandled byTypical cost
Cache missCPU cache to RAMHardwareAbout 50 to 100 nanoseconds
TLB missAddress translation cacheHardware page walker or kernelTens of nanoseconds
Minor page faultPage table entryKernelMicroseconds
Major page faultRAM to storageKernel and storage driverTens of microseconds to milliseconds
Segmentation faultProcess memory mapKernel, then signal to processProcess usually terminates
Page fault and related events

A TLB miss does not imply a page fault: if the page table entry is valid, the hardware simply refills the TLB. Only an invalid or insufficient entry raises the exception.

Measuring page faults and avoiding excessive ones

Page faults are measured with per-process fault counters, and the useful metric is the rate of major faults over time. On Linux, ps -o min_flt,maj_flt, /usr/bin/time -v, vmstat and perf stat -e page-faults expose them; Windows reports them in Task Manager, Resource Monitor and Performance Monitor counters.

A sustained high major fault rate combined with low CPU use usually indicates memory pressure. When the combined working set of running processes exceeds physical RAM, the system enters thrashing, spending most of its time servicing faults instead of running programs.

  • Add RAM or reduce the working set when major faults stay high under normal load.
  • Use huge pages for large, hot data structures to reduce TLB pressure and fault count.
  • Pre-touch or lock memory with mlock in latency-critical services so that faults happen at startup, not during requests.
  • Tune the page replacement policy and swappiness only after confirming that faults are the bottleneck.

Common misconceptions about page faults

The most widespread misconception is that every page fault signals a problem. A healthy desktop system routinely handles thousands of minor faults per second; only major faults at a sustained rate deserve attention.

  • A page fault is not the same as a segmentation fault; only invalid faults lead to one.
  • Free memory reported as low does not mean faults are frequent, because the page cache fills idle RAM on purpose.
  • Disabling swap does not remove major faults, since file-backed pages are still read from disk on demand.

Put simply

Programs see a large, tidy block of memory, but the computer keeps only part of it in fast RAM at any moment. When a program reaches for a piece that is not ready yet, the processor pauses it and asks the operating system to fetch that piece. That pause is a page fault.

It works like a library with a closed stack: you request a book, a librarian brings it from storage, and you continue reading. Most page faults are normal and quick; they become a problem only when the system must fetch from slow storage again and again.

In practice

An operations engineer investigates a reporting server with 16 GiB of RAM whose nightly job has slowed from 20 minutes to almost two hours. CPU usage sits near 15 percent, yet the machine feels unresponsive.

Running vmstat 1 shows heavy swap-in activity, and perf stat on the job reports more than 3,000 major page faults per second. The job loads a 12 GiB dataset while an in-memory cache on the same host holds another 8 GiB, so the combined working set exceeds physical memory and the system is thrashing.

The engineer caps the cache at 3 GiB and schedules it to shrink during the job window. Major faults fall to a few dozen per second, and the job finishes in 22 minutes again, without new hardware.