What is Virtual Memory
Virtual memory gives each process the illusion of having its own private, contiguous address space. Behind the scenes, the operating system and CPU hardware collaborate to map virtual addresses to physical RAM — and a process never sees (or can access) another process's memory.
How it works
Memory is divided into fixed-size pages, typically 4 KB each. Each process has a page table that maps its virtual page numbers to physical page frames in RAM. When the CPU accesses a virtual address, the MMU (memory management unit) translates it to a physical address using the page table.
This translation happens on every memory access, so it must be fast. The CPU caches recent translations in a TLB (translation lookaside buffer). A TLB hit is nearly free. A TLB miss requires walking the page table, which is slower.
Not all pages need to be in RAM at once. The OS can page out rarely used memory to disk and page in when it's accessed again. When a process touches a page that isn't in RAM, the MMU raises a page fault, the OS loads the page from disk, and the process resumes — never knowing the delay happened.
Virtual memory also enables:
- Isolation — a bug in one process cannot corrupt another's memory, because their page tables point to different physical frames.
- Overcommit — the OS can promise more virtual memory than physical RAM exists, relying on the fact that most programs don't use all their allocations at once.
- Shared libraries — multiple processes can map the same physical pages read-only, saving RAM.
Why it matters
Virtual memory is the foundation of modern process isolation. Without it, every program would need to coordinate memory addresses with every other program. Your stack, heap, and code all live within the virtual address space. Understanding virtual memory explains why processes can't read each other's data, why your system can run more programs than fit in RAM, and why page faults cause performance cliffs.
See How Memory Works for the full walkthrough.