What is Demand Paging?
Learn what demand paging is — page faults, lazy loading, and memory efficiency — with examples and operating systems interview questions.
Expected Interview Answer
Demand paging is a virtual memory technique where a page is loaded into physical memory only when a process actually references it, rather than loading the whole program up front.
When a process starts, the OS creates a page table but marks every page as not-present, so no code or data is actually in RAM yet. The first time a page is accessed, the memory management unit raises a page fault, the OS locates the page on disk, loads it into a free frame, updates the page table, and resumes the instruction. This lets programs start faster and run with a memory footprint far smaller than their total size, at the cost of occasional page-fault latency. Effective design keeps the page-fault rate low through good locality of reference and an efficient replacement policy such as LRU.
- Faster process start-up — only the needed pages load
- Lower physical memory usage than loading the whole program
- Allows programs larger than physical memory to run
- Pairs naturally with page-replacement algorithms like LRU
AI Mentor Explanation
Demand paging is like a touring squad that only flies in a player the moment the coach actually needs them for a specific match, instead of bringing the entire roster to every venue upfront. If an uncalled player is suddenly required, there is a delay while they are flown in — that delay is the page fault. Once they arrive, they are added to the matchday squad list, just as a page fault installs the entry in the page table. This keeps travel costs low while still covering every match.
Step-by-Step Explanation
Step 1
Lazy page table
On process start, the page table exists but every entry is marked not-present — nothing is loaded yet.
Step 2
Reference triggers a fault
The first access to a page causes the MMU to raise a page fault since it is not in a physical frame.
Step 3
OS loads the page
The page fault handler locates the page on disk, loads it into a free frame, and updates the page table entry.
Step 4
Resume execution
The faulting instruction is retried and now succeeds because the page is present in memory.
What Interviewer Expects
- A clear statement that pages load lazily, on first reference
- Understanding of the page-fault mechanism and handler steps
- Awareness of the trade-off between start-up speed and fault latency
- A mention of page-replacement policy relevance (e.g. LRU)
Common Mistakes
- Confusing demand paging with pre-paging or swapping the whole process
- Thinking a page fault always means an error or crash
- Forgetting that page faults have a real performance cost
- Not connecting demand paging to virtual memory oversubscription
Best Answer (HR Friendly)
“Demand paging means the operating system only loads a piece of a program into memory right when it is actually used, instead of loading everything upfront. This makes programs start faster and use less memory, though the very first time a piece is needed there is a small delay while it gets fetched.”
Code Example
struct page_table_entry {
int frame_number;
int present; /* 0 = not loaded yet, 1 = in physical memory */
};
void handle_page_fault(struct page_table_entry *pt, int page_num) {
if (pt[page_num].present)
return; /* not actually a fault */
int frame = allocate_free_frame(); /* find/evict a frame */
load_page_from_disk(page_num, frame); /* demand-load it now */
pt[page_num].frame_number = frame;
pt[page_num].present = 1; /* mark present */
}
/* CPU retries the faulting instruction after this returns */Follow-up Questions
- What is the difference between demand paging and pre-paging?
- What happens when there are no free frames during a page fault?
- How does thrashing relate to demand paging?
- What is a minor vs major page fault?
MCQ Practice
1. In demand paging, when is a page first loaded into memory?
Demand paging loads a page lazily, only on its first reference, triggering a page fault.
2. What raises a page fault in a demand-paged system?
A page fault occurs when the MMU finds the referenced page is marked not-present in the page table.
3. What is a key benefit of demand paging?
Only needed pages are loaded, so processes start faster and use less physical memory than loading everything upfront.
Flash Cards
What is demand paging? — Loading a page into memory only when it is actually referenced, not upfront.
What triggers the page load? — A page fault raised by the MMU when an absent page is accessed.
Main benefit of demand paging? — Faster start-up and lower memory footprint than loading the whole program.
Main cost of demand paging? — Page-fault latency the first time each page is touched.