24/32 · 6 min
Running a program bigger than your RAM
Paging removed the need for memory to be contiguous. Virtual memory removes the need for it to all be present.
A page that is not in RAM lives on disk. Touch it and the hardware raises a page fault, the kernel fetches it, and your instruction resumes as though nothing happened.
Replacement
RAM is full and a page is needed. Something must be evicted. Consider the reference string 7, 0, 1, 2, 0, 3, 0, 4 with three frames:
FIFO evicts the oldest page loaded. Simple, and it can evict a page that is in constant use just because it arrived first.
LRU evicts the page unused for longest. Close to optimal, and expensive: it needs a timestamp on every access.
Optimal evicts the page that will not be needed for longest. It requires knowing the future, so it exists only as a benchmark for the other two.
| Algorithm | Faults here | Practicality |
|---|---|---|
| FIFO | 6 | Easy, and suffers Belady's anomaly |
| LRU | 5 | Good, needs hardware help |
| Optimal | 4 | Impossible, used as the yardstick |
Belady's anomaly
FIFO can produce more faults with more frames. It is not intuition failing: it is a real property of FIFO, and it is why "add RAM" is not always the answer. LRU cannot do this.
Thrashing
Give a process fewer frames than its working set, the pages it is actively using, and it faults on almost every access. It spends its life waiting for the disk, the operating system sees low processor use and admits more processes, and everything gets worse.
That is thrashing, and it is what a laptop is doing when memory runs out: the disk light is constant, the fan is quiet, and nothing responds.