OS Notes
Complete guide to virtual memory — how it works, demand paging foundation, page fault handling, benefits of virtual memory, and why programs can use more memory than physically available.
Introduction
Here is a mind-bending fact: your computer with 8 GB of RAM can run programs that collectively need 20 GB of memory. How? The answer is virtual memory — one of the most brilliant inventions in computer science. Virtual memory creates the illusion that each process has access to a large, private, contiguous address space, even when physical RAM is limited.
Think of it like a magician's trick with books and a desk. Your desk (RAM) can hold only 5 books at a time. But you have access to 100 books on a nearby bookshelf (disk). When you need a book that is not on your desk, you put one away and bring the needed one from the shelf. If you do this quickly enough, it looks like you have all 100 books available simultaneously. Virtual memory does this with memory pages and disk storage.
How Virtual Memory Works
Virtual memory separates the logical address space (what the process sees) from physical memory (actual RAM). Not all pages of a process need to be in physical memory at all times. Pages that are not currently needed reside on disk (in swap space), and are brought into RAM only when accessed.
| Process Logical Address Space (4 GB) | Physical RAM (1 GB): |
| ┌────────┐ Page 0 ───────────────────── | ┌────────┐ Frame 0 |
| ├────────┤ Page 3 ───────────────────── | ├────────┤ Frame 3 (Page 3) |
| ├────────┤ Page 5 ───────────────────── | └────────┘ |
Page Fault Mechanism
When a process accesses a page that is not in RAM (its valid bit is 0 in the page table), a page fault occurs:
| 2. MMU checks page table | Valid bit = 0 (page not in RAM) |
| 4. OS checks | Is this a valid address? |
| - Invalid | Segmentation fault (kill process) |
| - Valid but on disk | Continue to step 5 |
| - If no free frame | Run page replacement algorithm |
| 7. OS updates page table | frame number = new frame, valid = 1 |
Benefits of Virtual Memory
1. Programs Larger Than Physical Memory
A 4 GB game can run on a system with 2 GB RAM because only the currently active portions need to be in memory.
2. More Processes Simultaneously
With only active pages in RAM, more processes fit in memory at the same time, improving CPU utilization.
3. Simplified Programming
Programmers do not need to worry about physical memory size or manually manage overlays. Every process gets a large, uniform address space.
4. Memory Protection
Each process has its own page table. It is impossible for one process to access another's memory without the OS's explicit permission.
5. Shared Libraries
Multiple processes can map the same physical frames (shared library code) into their address spaces, saving memory.
Performance Considerations
Virtual memory works well because of the principle of locality:
- Temporal locality: Recently accessed pages are likely to be accessed again soon
- Spatial locality: Pages near recently accessed pages are likely to be accessed soon
If a program accesses memory randomly across its entire address space, page faults would be constant and performance would collapse. Fortunately, real programs exhibit strong locality.
Effective Access Time with Page Faults
| Memory access | 100 ns |
| Page fault handling | 8 ms (8,000,000 ns) — includes disk I/O! |
| Example | p = 0.001 (1 fault per 1000 accesses) |
This shows why page replacement algorithms and working set management are critical — keeping the page fault rate extremely low is essential.
Copy-on-Write (COW)
When fork() creates a child process, virtual memory enables an optimization called Copy-on-Write. Instead of copying all parent pages immediately, parent and child share the same physical frames (marked read-only). Only when either process tries to WRITE to a shared page does the OS create a private copy of that specific page.
After fork() with COW
Parent Page Table: Page 5 → Frame 12 (read-only)
Child Page Table: Page 5 → Frame 12 (read-only) ← Same frame!
When child writes to Page 5
1. Write triggers protection fault (page is read-only)
2. OS creates copy: Frame 12 → new Frame 25
3. Child's Page 5 now → Frame 25 (read-write)
4. Parent's Page 5 stays → Frame 12 (back to read-write)
This makes fork() extremely fast — only modified pages are ever actually copied.
Memory-Mapped Files
Virtual memory also enables memory-mapped files. Instead of using read()/write() system calls, a file is mapped directly into the process's address space. Accessing the mapped memory region automatically reads from or writes to the file.
Real-World Analogy
Virtual memory is like a student studying for exams with a small desk (RAM) and a large bookshelf (disk). You can only have a few books open on your desk at once. When you need a different book, you close one on the desk and grab the new one from the shelf. If you organize well (good locality), you rarely need to get up (few page faults). If you try to read from 10 books randomly (poor locality), you spend more time swapping books than actually studying (thrashing).
Key Takeaways
- Virtual memory allows processes to use more memory than physically available in RAM
- Pages not currently needed reside on disk and are loaded on demand (page faults)
- The principle of locality ensures page faults are rare in well-behaved programs
- Even a small page fault rate dramatically impacts performance due to disk I/O latency
- Copy-on-Write makes fork() efficient by sharing pages until modification
- Memory-mapped files allow file access through memory operations
- Virtual memory provides isolation, sharing, and simplified programming
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Virtual Memory — Operating Systems.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Operating Systems topic.
Search Terms
operating-systems, operating systems, operating, systems, memory, management, virtual, virtual memory — operating systems
Related Operating Systems Topics