What is Virtual Memory?
Learn what virtual memory is — page tables, page faults, and thrashing — with examples and operating systems interview questions answered.
Expected Interview Answer
Virtual memory is a technique that gives each process the illusion of a large, private, contiguous address space by mapping virtual addresses to physical RAM (and disk) through page tables, decoupling what a program sees from where data actually lives.
Each process addresses memory using virtual addresses, which the memory management unit (MMU) translates to physical addresses via a page table; pages not currently needed can be evicted to a backing store on disk. This gives isolation between processes (one cannot see another’s memory), lets total virtual address space exceed physical RAM, and allows the OS to load only the pages actually used. When a process accesses a page not currently in RAM, a page fault occurs, and the OS fetches it from disk, possibly evicting another page first. The main cost is that page faults are slow, and heavy paging under memory pressure leads to thrashing, where the system spends more time swapping pages than doing useful work.
- Isolates each process into its own private address space
- Lets programs use more memory than physically installed RAM
- Loads only the pages actually needed, saving physical memory
- Simplifies programming — no manual physical memory management
AI Mentor Explanation
Virtual memory is like a stadium issuing every fan a seat number from a master seating chart, even though not every seat is physically built yet — staff (the MMU) map the chart number to a real seat only when the fan arrives. If a section is not currently in use, it can be repurposed for storage (paged out) and rebuilt when needed (paged in). A fan turning up to a not-yet-built seat causes a delay while it is prepared — exactly like a page fault.
Step-by-Step Explanation
Step 1
Virtual address generated
A process references memory using a virtual address, unaware of physical location.
Step 2
MMU translation
The memory management unit consults the page table to translate the virtual address to a physical frame.
Step 3
Page present check
If the page is in RAM, access proceeds; if not, a page fault is raised.
Step 4
Page fault handling
The OS loads the page from disk into a free (or evicted) frame and resumes the instruction.
What Interviewer Expects
- Virtual-to-physical address translation via page tables
- Isolation and address-space benefits for processes
- What a page fault is and how it is handled
- Awareness of thrashing as the downside of overcommitted memory
Common Mistakes
- Confusing virtual memory with swap space alone
- Thinking virtual memory gives unlimited free performance
- Not knowing what triggers a page fault
- Ignoring thrashing as a risk under memory pressure
Best Answer (HR Friendly)
“Virtual memory lets each program think it has its own large, private chunk of memory, even though the operating system is really juggling limited physical RAM behind the scenes, pulling data in from disk only when it is actually needed. It is what makes multiple programs run safely and use more memory than is physically installed, at the cost of occasional slow fetches called page faults.”
Code Example
/* Virtual address = page_number : offset */
unsigned page_number = virtual_addr / PAGE_SIZE;
unsigned offset = virtual_addr % PAGE_SIZE;
if (!page_table[page_number].present) {
handle_page_fault(page_number); /* fetch page from disk into a free frame */
}
unsigned physical_addr =
page_table[page_number].frame * PAGE_SIZE + offset;Follow-up Questions
- What is a page fault and how expensive is it?
- What is thrashing and how does an OS mitigate it?
- How does the translation lookaside buffer (TLB) speed up address translation?
- What page replacement algorithms exist and how do they differ?
MCQ Practice
1. What component translates a virtual address to a physical address?
The MMU consults the page table to translate each virtual address into a physical frame address.
2. A page fault occurs when?
If the requested page is not present in RAM, the hardware raises a page fault so the OS can load it from disk.
3. Excessive paging under memory pressure is called?
Thrashing occurs when the system spends more time swapping pages in and out than executing actual work.
Flash Cards
What is virtual memory? — A technique giving each process a private address space, mapped to physical RAM/disk via page tables.
What translates virtual to physical addresses? — The memory management unit (MMU), using the page table.
What is a page fault? — A trap raised when a referenced page is not currently in physical memory.
What is thrashing? — Excessive paging where the system spends more time swapping than doing useful work.