Virtual memory
Operating Systems ยท 10 interview questions
Virtual memory lets a process address more memory than physically exists by keeping only the pages it's actively using in RAM and the rest on disk. Pages are loaded on demand: touching a page that isn't resident raises a page fault, the OS fetches it, and the instruction restarts.
When memory is full, something must be evicted, which is what page replacement algorithms decide. Optimal evicts the page used furthest in the future and is unimplementable, existing only as a benchmark. FIFO is simple and can behave pathologically. LRU approximates optimal well and is what real systems approximate with clock or second-chance algorithms.
Thrashing is the failure mode: so little memory per process that pages are evicted before they're reused, so the system spends its time paging rather than executing. The fix is to reduce the degree of multiprogramming, not to add more paging.
Virtual memory interview questions
- What happens on a page fault?
- The MMU traps to the OS, which finds the page on disk, evicts a frame if needed, reads the page in, updates the page table, and restarts the faulting instruction. The process is blocked throughout.
- What is demand paging?
- Loading pages only when first accessed rather than up front. Programs start faster and never-used pages are never read, at the cost of faults early in execution.
- What is Belady's anomaly?
- Giving a process more frames can increase the number of page faults under FIFO โ the opposite of what you'd expect. It's a property of FIFO, not of paging in general.
- Does LRU suffer from Belady's anomaly?
- No. LRU is a stack algorithm: the pages resident with n frames are always a subset of those resident with n+1, so adding frames can never increase faults. Optimal has the same property.
- Why they ask: The 'why' is the answer here โ stack algorithms are immune by construction.
- Why do we study the optimal replacement algorithm if it can't be implemented?
- As a lower bound. It requires knowing future references, which is impossible at run time, but it tells you how close a practical algorithm gets.
- How does the clock (second-chance) algorithm approximate LRU?
- Frames sit in a circular list with a reference bit set on access. The hand sweeps; a frame with the bit set gets it cleared and is spared, one with it clear is evicted. It gives LRU-like behaviour without timestamping every access.
- What is thrashing and what causes it?
- Spending most of the time paging rather than executing, because processes don't have enough frames to hold their active working sets. Adding processes makes it worse, so the fix is to reduce the degree of multiprogramming or add physical memory.
- What is the working set model?
- The set of pages a process has referenced in a recent window. If every process's working set fits in memory, paging is rare; if the sum exceeds available frames, thrashing follows. It gives the OS a criterion for how many processes to admit.
- What does the dirty bit save?
- A disk write. A page not modified since being loaded already matches its copy on disk, so eviction can just drop it. Only dirty pages must be written back.
- Why does virtual memory work at all in practice?
- Locality of reference. Programs concentrate accesses in small regions over short periods โ loops, nearby data โ so a modest resident set covers the overwhelming majority of accesses.
You'll forget most of this by next week
That's not a discipline problem, it's how memory works. In the app these come back on an expanding schedule โ right before you'd lose them.
Start free for 7 days