Introduction
Modern operating systems use virtual memory to give each process the illusion of a large, contiguous address space while managing a limited amount of physical RAM. Virtual memory improves security, isolation, and memory utilization.
Why virtual memory?
- Isolation: each process sees its own address space, preventing accidental interference.
- Overcommit: systems can run processes whose combined memory demands exceed physical RAM.
- Efficient sharing: common pages (libraries) can be mapped into multiple processes.
- Simplified programming model: programmers can use large address ranges without manually managing physical memory.
Core Concepts
- Virtual Address (VA)
- Address used by a program; translated by the MMU (Memory Management Unit) to a physical address.
- Physical Address (PA)
- Actual location in RAM (or physical memory device).
- Page
- Fixed-size block of virtual memory, commonly 4KB, 2MB (huge pages), or 1GB (gigantic pages).
- Frame
- Corresponding fixed-size block in physical memory.
- Page Table
- Data structure the OS keeps to map virtual pages → physical frames (or indicate a page is on disk).
- Page Fault
- Hardware/OS event triggered when a process accesses a virtual page not currently mapped to physical memory.
Paging
Paging is the technique of dividing memory into pages and mapping those pages to frames. It avoids fragmentation
associated with variable-sized allocation and simplifies allocation: the OS manages equal-sized units.
/* Virtual page
VPN 0x0001 -> Frame 0xA2
VPN 0x0002 -> Frame 0xA3
VPN 0x0003 -> not present (on disk)
Page faults & handling
When a page fault occurs, the OS kernel takes control, determines whether the access is valid, loads the page from disk (swap or file), updates the page table, and resumes the process. If no free frame is available, the OS must choose a victim page and evict it.
Replacement algorithms
Common strategies include:
- LRU (Least Recently Used): evict the page that hasn’t been used for the longest time.
- FIFO: first-in-first-out; simple but can suffer Belady’s anomaly.
- Clock (approximate LRU): efficient and widely used; uses a reference bit and a pointer (clock hand).
- Optimal (Belady): evict the page that will not be used for the longest future time — impossible to implement exactly, used as a benchmark.
Performance considerations
Page faults are expensive — they involve disk I/O. Reduce fault rates by:
- Using enough RAM for working sets
- Reducing page thrashing via better scheduling or memory limits
- Using large pages for memory-intensive workloads (TLB efficiency)
TLB: Translation Lookaside Buffer
The TLB is a small cache in the MMU storing recent virtual→physical translations. A TLB hit avoids a page table walk,
dramatically improving performance. TLB misses are handled by hardware or by the OS, depending on the architecture.
Security & isolation
Virtual memory enforces process isolation: one process cannot directly read or write another’s memory because their virtual pages map to different frames.
OS-enforced permissions (read/write/execute) on page table entries also help protect against code injection and buffer overflows.
Copy-on-Write (COW)
COW allows parent and child processes (after fork()) to share physical pages as read-only until one process writes,
at which point the OS duplicates the page. This reduces memory usage and speeds up process creation.
Paging vs Segmentation
| Feature | Paging | Segmentation |
|---|---|---|
| Unit | Fixed-size pages | Variable-size segments |
| Fragmentation | Internal fragmentation | External fragmentation |
| Protection | Simple, per-page | Per-segment (logical) |
| Use cases | General-purpose OS memory management | Language/runtime level (rare alone in modern OSes) |
Practical questions & examples
How big is a page?
Page sizes vary by architecture. Typical defaults are 4KB for general use and larger sizes (2MB, 1GB) for huge pages. Bigger pages
reduce TLB pressure but can cause more internal fragmentation.
Should I enable swap?
Swap provides breathing room when RAM is exhausted, but relying heavily on swap degrades performance. For desktop/workstation systems, a modest swap
helps stability; for latency-sensitive servers, prefer ample RAM and tuned memory limits.
Example: what happens on a page fault
- Process accesses VA → MMU checks TLB; if miss, walks the page table.
- If the page table entry shows “not present”, CPU triggers a page fault exception.
- Kernel page-fault handler validates the access. If invalid → segmentation fault (SIGSEGV).
- If valid but on disk: allocate a free frame (possibly evicting another page), read page from disk into frame.
- Update page table and TLB, then retry the faulting instruction.
Note: The exact flow depends on CPU/MMU architecture — x86, ARM, RISC-V have different details like multi-level page tables and nested page tables for virtualization.
Further reading & resources
- Operating Systems: Three Easy Pieces (chapter on virtual memory)
- Intel / ARM architecture manuals (MMU & paging)
- Linux kernel documentation: memory management
Summary
Virtual memory and paging are fundamental OS concepts that provide isolation, efficient sharing, and flexible use of limited physical memory.
Understanding page tables, page faults, replacement strategies, and TLB behavior helps you reason about performance, design systems, and debug memory-related issues.
FAQ
- Can virtual memory be disabled?
- On most systems you can boot with flags that limit or disable swap, but fully disabling virtual memory (no paging) is uncommon and restricts many OS features.
- What is thrashing?
- Thrashing occurs when the system spends most time swapping pages in/out instead of executing useful work. Fix by increasing RAM, reducing working set, or changing scheduling.