Page Faults, Replacement and Thrashing
Follow a page fault from trap to retry, compare FIFO, LRU and optimal replacement on real reference strings, and see how thrashing brings systems to a crawl.
Welcome back!
Step 1 of 12Learn
Loading only what you use
When you start a large program, say a 300 MB photo editor, the OS doesn't read all 300 MB from disk into RAM. It sets up the page table with every page marked not present and starts the program. This is demand paging: pages are loaded only when the program actually touches them.
The first time the program accesses a non-present page, the MMU raises a page fault, an exception that hands control to the kernel. The kernel then:
- Checks whether the address is valid for this process. If it isn't, for example a stray pointer, the process gets a segmentation fault and is usually terminated.
- Finds a free physical frame, or picks a victim page to evict if none is free. If the victim is dirty, it must first be written back to disk.
- Reads the needed page from disk (the program file or the swap area) into the frame. The faulting process waits in the blocked state while other processes run.
- Updates the page table entry to point to the frame and marks it present.
- Restarts the instruction that faulted. This time it succeeds, and the program never knows anything happened.
Demand paging makes programs start quickly and saves memory, because large parts of most programs, such as rarely used menus, error handlers and features, are never touched at all.
Not every page fault needs the disk. A minor fault happens when the page is already in RAM, such as a shared library page used by another process, and only the mapping needs to be set up. A major fault requires disk I/O and is thousands of times slower.
Step 2 of 12Practice · Put in order
Put the steps the kernel takes to handle a major page fault in order.
Drag the items, or use the arrow buttons, to put them in order.
- The kernel finds a free frame or evicts a victim page
- The faulting instruction is restarted
- The MMU raises a page fault exception
- The page is read from disk into the frame
- The page table entry is updated and marked present
- The kernel checks that the address is valid for the process
Step 3 of 12Learn
Why page faults are so expensive
Page faults are where the memory hierarchy's speed gaps become painful. A memory access takes around 100 nanoseconds. Servicing a major page fault from an SSD takes tens to hundreds of microseconds, and from a spinning hard disk several milliseconds, tens of thousands of times longer.
The effective access time with page faults is:
EAT = (1 − p) × memory access time + p × page fault service time
where p is the probability that an access causes a major page fault.
Take a 100 ns memory access and an 8 ms (8,000,000 ns) fault service time from a hard disk. If just one access in 1,000 faults (p = 0.001): EAT ≈ 0.999 × 100 + 0.001 × 8,000,000 ≈ 100 + 8,000 = about 8,100 ns. The machine is effectively running 80 times slower than its RAM allows.
To keep the slowdown under 10% (EAT under 110 ns), p must be below about 1 in 800,000. That's why systems work so hard to keep the pages a program actively uses, its working set, in RAM, and why adding RAM to a machine that is constantly paging makes such a dramatic difference. SSDs lower the fault cost considerably, but even they are hundreds of times slower than RAM.
Step 4 of 12Practice · Diagram
How long things take (log scale)
Tap the cost that makes a single fault per 1,000 accesses slow a program about 80-fold.
Tap the shape that answers the question, then press Check.
- RAM access ~100 ns
100 ns
- SSD page fault ~100 µs
About 1,000 times a RAM access.
- Hard disk fault ~8 ms
About 80,000 times a RAM access.
- Register / L1 cache ~1 ns
1 ns
Step 5 of 12Explore
How page fault rate drives access time
x is the number of major page faults per million memory accesses. Memory access costs 100 ns.
- Access time at 10 faults per million
- 180 ns
- Access time at 1,000 faults per million
- 8,100 ns
Step 6 of 12Learn
Choosing a victim: FIFO, optimal and LRU
When RAM is full and a page must be loaded, the OS has to evict one. A good page replacement algorithm evicts pages that won't be needed soon. Algorithms are compared by counting faults on a reference string, the sequence of pages a program accesses.
- FIFO (first in, first out) evicts the page that has been in memory longest. Simple, but an old page might be heavily used, like the page holding a program's main loop.
- Optimal (OPT, or Bélády's algorithm) evicts the page whose next use is furthest in the future. It gives the fewest possible faults but requires knowing the future, so it's used only as a benchmark.
- LRU (least recently used) evicts the page unused for the longest time, betting that the recent past predicts the near future. It usually performs close to optimal.
Work through the string 7, 0, 1, 2, 0, 3, 0, 4 with 3 frames under FIFO. The first three references fault and fill the frames: [7, 0, 1]. Page 2 faults and evicts 7, the oldest: [0, 1, 2]. Page 0 hits. Page 3 faults and evicts 0, now the oldest, even though it was just used: [1, 2, 3]. Page 0 faults again and evicts 1. Page 4 faults and evicts 2. Total: 7 faults in 8 references.
Exact LRU needs a timestamp update on every memory access, which is too expensive. Real kernels approximate it with the hardware's accessed bit, for example with the clock (second-chance) algorithm: sweep around the frames like a clock hand, clearing accessed bits, and evict the first page whose bit was already clear.
Step 7 of 12Practice · Chart
Page faults on a classic 20-reference string, 3 frames
Tap the algorithm that real kernels cannot implement exactly.
Tap the bar that answers the question, then press Check.
Step 8 of 12Practice · Work it out
Using LRU with 3 frames that start empty, how many page faults occur for the reference string 7, 0, 1, 2, 0, 3, 0, 4?
Step 9 of 12Note
Step 10 of 12Learn
Thrashing and the working set
Every program has a working set: the pages it actively uses over a short window of time. A text editor's working set might be a few dozen megabytes; a large database query's might be gigabytes.
If the sum of all running processes' working sets fits in RAM, page faults are rare and the system runs well. If it doesn't, something ugly happens. A process faults, and the kernel evicts a page that another process needs. That process soon faults and evicts a page the first process needs. Soon most processes spend their time waiting for the disk, the CPU sits mostly idle, and the system does almost no useful work. This is thrashing.
A classic trap made it worse on early systems: the OS noticed low CPU use and started more processes to keep the CPU busy, which increased memory pressure and deepened the thrashing.
The remedies all reduce demand or increase supply:
- Add RAM, the most direct fix.
- Run fewer processes at once, or suspend some entirely and swap them out, so the rest have room for their working sets.
- Use page-fault-frequency control: if a process faults too often, give it more frames; if it rarely faults, take some away.
- On Linux, the out-of-memory (OOM) killer terminates a process as a last resort when memory is exhausted.
On a modern laptop, thrashing appears as a sudden, dramatic slowdown when too many browser tabs and apps are open, with the disk constantly busy.
Step 11 of 12Practice · Chart
Thrashing: more processes, less work done
Tap the point after which adding processes starts to hurt.
Tap the point that answers the question, then press Check.
| Processes running | CPU utilization |
|---|---|
| 2 | 35% |
| 4 | 60% |
| 8 | 85% |
| 12 | 92% |
| 16 | 70% |
| 20 | 30% |
| 24 | 15% |
Step 12 of 12Practice · What would you do?
What would you do?
A server runs 20 worker processes. CPU use has dropped to 15%, the disk is 100% busy, and requests are taking ten times longer than usual. An engineer proposes starting 10 more workers to use the idle CPU. What do you recommend?
Lesson complete!