A page replacement algorithm is the policy an operating system uses to choose which resident page to evict from physical memory when a page fault occurs and no free page frame is available. In memory management, it is the component that turns a fixed amount of RAM into the illusion of a much larger virtual address space, and its quality largely decides whether a loaded system stays responsive or slides into thrashing.
Modern operating systems such as Linux, Windows and macOS do not run a single textbook algorithm. They combine approximations of least recently used behavior, background reclaim threads and separate treatment of file-backed and anonymous memory, but every production design still answers the same question: which page is least likely to be needed soon.
How page replacement works during a page fault
Page replacement runs when the memory management unit reports a reference to a page that is not resident and the kernel finds no free frame. The kernel selects a victim page, writes it to backing storage if it is dirty, updates the page table entry to mark it non-resident, invalidates any cached translation in the translation lookaside buffer, and finally loads the requested page into the reclaimed frame.
The cost of a wrong choice is asymmetric. A memory access takes on the order of 100 nanoseconds, while servicing a major fault from an SSD takes tens to hundreds of microseconds and from a hard disk several milliseconds. Even a fault rate of one in a million accesses can therefore double effective memory latency, which is why replacement quality matters far more than the cost of the algorithm itself.
The following table lists the hardware and kernel signals that replacement policies rely on and what each one tells the kernel.
| Signal | Source | What it indicates |
|---|---|---|
| Referenced (accessed) bit | Set by the MMU on any access | Page was used since the bit was last cleared |
| Dirty (modified) bit | Set by the MMU on a write | Page must be written back before eviction |
| Page type | Kernel bookkeeping | File-backed pages can be dropped, anonymous pages need swap |
| Pinned or locked flag | Kernel or mlock call | Page must not be evicted at all |
| Fault history | Kernel shadow entries | Page was evicted recently and is being refaulted |
Classic page replacement algorithms
The textbook algorithms differ mainly in how much history they keep and how expensive that history is to maintain. The theoretical benchmark is the optimal algorithm (OPT or MIN), which evicts the page whose next use lies furthest in the future; it cannot be implemented because it requires knowledge of future references, but it gives a lower bound for comparing real policies on recorded traces.
The table below compares the algorithms most often discussed in operating systems courses and literature, together with their practical standing.
| Algorithm | Eviction rule | Overhead | Practical standing |
|---|---|---|---|
| Optimal (OPT) | Page used furthest in the future | Needs future knowledge | Benchmark only |
| FIFO | Oldest loaded page | Very low | Poor alone, suffers Belady’s anomaly |
| LRU | Least recently used page | High if exact | Good model, approximated in practice |
| Clock (second chance) | First page with referenced bit clear | Low | Widely used basis |
| NRU | Lowest class by referenced and dirty bits | Low | Simple, coarse |
| LFU | Least frequently used page | Moderate | Keeps stale popular pages too long |
| WSClock | Clock plus working set age | Moderate | Good for multiprogramming |
Exact LRU requires updating a timestamp or reordering a list on every memory access, which hardware does not support for main memory. That is why the Clock algorithm dominates real kernels: it sweeps a circular list, clears referenced bits as it passes, and evicts the first page found with the bit still clear, giving LRU-like behavior at a fraction of the cost.
Replacement in modern operating systems
Production kernels refine the classic ideas to resist scan pollution, where a single large sequential read pushes the entire hot working set out of memory. The common remedy is to keep separate active and inactive lists so that a page must prove itself with a second access before it is treated as hot.
- Linux historically used active and inactive LRU lists per memory cgroup, split into file-backed and anonymous pages, and since version 6.1 offers the multi-generational LRU (MGLRU), which ages pages across several generations and scans page tables more efficiently.
- Windows manages a working set per process, trims pages to standby and modified lists, and reuses standby pages without disk access if they are referenced again before being repurposed.
- Database buffer pools and storage caches often use ARC, 2Q or LRU-K, which weigh recency and frequency together, although these designs are rarer in general-purpose kernels.
Kernels also distinguish global replacement, where any process can lose frames to another, from local replacement, where a process replaces only its own pages. Global policies use memory more efficiently, while local policies isolate workloads; container memory limits are a modern form of local reclaim boundary.
Choosing and tuning replacement behavior
Administrators rarely select an algorithm directly, but they tune the inputs that the kernel policy uses. The decision table below maps common situations to adjustments and the reasoning behind them.
| Situation | Recommended adjustment | Reason |
|---|---|---|
| Interactive desktop under pressure | Enable MGLRU or keep defaults | Better protection of active pages |
| Database with its own cache | Lower swappiness, use direct I/O | Avoids double caching and swap storms |
| Latency-critical service | Lock hot memory with mlock | Prevents eviction of critical pages |
| Many containers on one host | Set memory limits and reservations | Confines reclaim to each workload |
| Large sequential backups | Use read-ahead hints or fadvise | Stops scans from flushing the cache |
On Linux, the swappiness parameter biases reclaim between dropping file pages and swapping anonymous pages. Its default of 60 suits general use; values near 10 favor keeping application memory resident, while higher values suit systems with fast swap such as zram.
Limitations and common misconceptions
No replacement algorithm can compensate for a working set larger than physical memory. When the sum of active working sets exceeds RAM, every policy degrades into constant faulting, and the only real fixes are adding memory, reducing load or killing processes through an out of memory handler.
| Symptom | Likely cause | Fix |
|---|---|---|
| High major fault rate, idle CPU | Working set exceeds RAM | Add memory or reduce concurrency |
| Cache emptied after a backup job | Scan pollution | Use fadvise or MGLRU |
| More frames, more faults | Belady’s anomaly under FIFO | Use a stack algorithm such as LRU |
| Swap use with plenty of free RAM | Reclaim of cold anonymous pages | Usually normal, adjust swappiness |
A frequent misconception is that swap use always signals a problem. Moving genuinely cold anonymous pages to swap frees frames for the page cache and often improves overall performance. Another misconception is that LRU is always best: for cyclic access patterns slightly larger than memory, LRU evicts exactly the page needed next, and random or frequency-aware policies perform better.
Explained without jargon
Think of a small desk and a large bookshelf across the room. You can only keep a few books open on the desk, so when you need a new one and the desk is full, you must carry one back to the shelf. A page replacement algorithm is the rule for deciding which book goes back.
A good rule returns the book you are least likely to open again soon, often the one you have not touched for the longest time. A bad rule keeps sending back the book you need next, and you spend all day walking instead of reading. Computers face the same choice with memory, and a smart choice keeps the machine fast.
A typical use case
A hosting team runs a Linux server with 64 GB of RAM that serves a web application and also runs a nightly backup reading about 500 GB of files. Every morning, page load times rise from about 80 milliseconds to over 400 milliseconds for the first hour, because the sequential backup read has pushed the application’s hot pages and cached files out of memory.
The team enables the multi-generational LRU, adds fadvise hints with the POSIX_FADV_DONTNEED flag to the backup tool so it drops pages after reading them, and lowers swappiness from 60 to 20. After the change, morning response times stay near 90 milliseconds, the major fault rate during the first hour falls by roughly an order of magnitude, and no extra memory had to be purchased.