Virtual memory
How every program can believe it owns the whole memory, starting at address 0. Follow the address 0x1234 through a page table, out to disk and back.
In 30 seconds
Programs never use real memory locations. Each process gets its own virtual address space, and the processor translates every address into a location in RAM using a table the operating system keeps for that process.
Because each process has its own table, two programs can use the same address without ever touching each other’s data.
The table can also say “not in RAM”. Pages that are not in use can be parked on disk and fetched back when touched, an event called a page fault.
Virtual memory, step by step
A toy machine with 4 pages per process and 8 frames. Real page tables have several levels and cover billions of pages, a cache called the TLB speeds up the lookup, and timings are rough orders of magnitude.
- Step 1: Each process, its own memory. Two programs are running. Each behaves as if it had the whole memory to itself, numbered from address 0. Both even use the very same address, 0x1234, and find different values there. This works because the addresses a program uses are virtual: made-up numbers that the processor translates to real locations in RAM.
- Step 2: Addresses split into pages. Memory is managed in fixed chunks of 4 KB (0x1000 bytes) called pages. With that size, the last three hex digits of an address say how far into a page it lies, the offset, and the digits in front are the page number. So 0x1234 is byte 0x234 of page 1.
- Step 3: The page table maps to RAM. RAM is cut into chunks of the same size, called frames. For each process the operating system keeps a page table: for every page, the frame that holds it. On each memory access the processor looks up the page number, swaps in the frame number and keeps the offset. A’s page 1 is in frame 2, so 0x1234 really means 0x2234.
- Step 4: Same address, separate frames. Process B has a page table of its own, and its page 1 is in frame 6. The same virtual address therefore leads to a different place in RAM. More importantly, no entry in B’s table points at a frame of A, so B has no address that could reach A’s data. The page tables are what keep processes isolated.
- Step 5: Swapping: pages on disk. All eight frames are taken and B needs one more page. The operating system picks a page that has not been used lately, here A’s page 2, writes it to a swap area on disk and marks it “on disk” in A’s table. Frame 3 now serves B. Together the processes can use more memory than the machine has RAM.
- Step 6: Page fault: fetch it back. Later A reads address 0x2008, on page 2. The table says the page is not in RAM, so the processor stops the instruction and raises a page fault. The operating system writes out frame 4’s old page, reads A’s page 2 from disk into frame 4, updates the table and restarts the instruction. A notices nothing but a delay.
- Step 7: What it buys, what it costs. Virtual memory gives every process a private address space, keeps processes apart, and lets them use more memory than there is RAM. The price is a lookup on every access, which the processor caches, and page faults: fetching a page from an SSD takes about 1000 times longer than reaching RAM, and far longer from a hard disk.
Try it yourself
Learn this first
Learn next
- Paging and page tablesHow a page table turns the addresses a program uses into real places in RAM. Intermediate
- Stack vs heap memoryFrames that come and go with each call, and blocks you request and must give back. Intermediate
- Virtual machines and hypervisorsOne physical computer divided into several complete, separate ones by a hypervisor. Beginner
Spotted a mistake on this page?