Operating Systems · Module 7 — Virtual Memory
Demand paging
A program almost never needs all of itself at once. gcc contains error-handling code for hundreds of situations that will not happen, support for languages Aisha is not using, and optimisation passes she did not enable.
Sign in to track your score
The compile job's full memory footprint is larger than what is free in the Nova-14's 8 GB. The browser has 14 tabs. The editor and music player are still running.
Aisha hits Build. It works.
Not "it works slowly". It starts almost immediately, and most of the compile job is not in RAM at all while it runs.
Why & what
The observation. A program almost never needs all of itself at once. gcc contains error-handling code for hundreds of situations that will not happen, support for languages Aisha is not using, and optimisation passes she did not enable.
Loading all of it would be wasted work.
Demand paging. Do not load a page until the moment it is first touched. "Demand" is literal: the page arrives because it was demanded, not before.
The valid bit. Each page table entry gets one extra bit.
- Valid — this page is in RAM, and here is its frame number.
- Invalid — this page is not in RAM. The frame number field means nothing.
That is the entire mechanism. The MMU already checks this bit on every access, so demand paging costs nothing extra when the page is present.
Where the missing pages live. In swap space — an area of the SSD the OS reserves for pages that are not currently in RAM. A page is either in a frame or in swap space, never nowhere.
What this buys.
- Programs start faster, because only the first few pages need loading.
- More processes fit in memory at once, because each one only occupies what it is actually using.
- A program can be larger than physical RAM. Nothing requires all of it to be resident simultaneously.
That last one is the reason for the phrase virtual memory: the address space a process sees is bigger than the RAM behind it.
One more bit, while we are here. Alongside the valid bit, each entry carries a dirty bit. The hardware sets it when the page is written to.
It matters for eviction. A clean page — one that was only ever read — already has an identical copy in swap space, so evicting it costs nothing; just mark the entry invalid. A dirty page has to be written back first, which is another 100 µs. Given a choice between two equally good victims, the OS evicts the clean one.
How it works
- The process starts with almost nothing loaded. Enough pages to run the first instructions, and no more.
- It touches a page. The MMU checks the valid bit as part of its normal translation.
- Valid — nothing special happens. The access completes in about 100 ns, exactly as in Module 6.
- Invalid — the MMU traps into the kernel. The instruction is stopped partway through, and the OS takes over. This is a page fault.
- The OS fetches the page from swap space, marks the entry valid, and lets the instruction run again. Cost: about 100 µs.

Common confusion
"A page fault is an error." It is not, and the name is unhelpful. A page fault is the normal, expected way a page gets loaded. Every program generates thousands of them while starting up. What is an error is touching an address that was never valid for the process at all — that is a segmentation fault, a different thing with a confusingly similar name.
"Virtual memory means using the disk as RAM." That is the mechanism, not the idea. The idea is that a process's address space is separated from physical memory, which Module 6 already established. Demand paging is what makes that separation useful — it lets the address space be bigger than the RAM.
"More swap space makes a machine faster." Swap space is a thousand times slower than RAM. Adding more of it lets you run more at once; it does not make anything faster, and past a point it makes things much worse, as Topic 7.4 shows.
Interview angle
"What is demand paging?" Load a page only when it is first referenced, using the valid bit in the page table to detect that moment. Then give the payoff: programs start faster and can be larger than physical memory.
"What is virtual memory?" The separation of a process's logical address space from physical RAM, which allows the address space to exceed the RAM available. Interviewers often accept a mechanism-level answer here, but the separation answer is stronger because it connects back to the MMU.
- 1.
What does the valid bit in a page table entry indicate?
- 2.
Why can a program be larger than the Nova-14's physical RAM?