Virtual Memory Explained
Virtual Memory: How Operating Systems Create the Illusion of Infinite Memory
Introduction
Modern computers allow users to run dozens of applications simultaneously—browsers with many tabs, code editors, games—all seemingly without running out of memory. This feels like an infinite resource, yet the machine typically has only a few physical sticks of RAM with a strict hardware limit. The sum of memory requested by running programs often exceeds available physical memory, yet they all coexist without crashing into each other. The solution is one of the most elegant design patterns in computer science: virtual memory, a layer of abstraction that fundamentally changes how software interacts with hardware. This note explores the evolution from early brute-force memory management to modern paging, covering base and bounds registers, segmentation, paging, the TLB, demand paging, page faults, and the breaking point known as thrashing.
Physical Addressing: The Brute-Force Era
In early computing, there was no abstraction—programs used physical addressing directly. When a programmer wrote code to store a value at address 500, the CPU sent a voltage signal directly to pin number 500 on the memory chip. This was direct, fast, but disastrous for multitasking.
The Problem of Ownership
When two programs ran simultaneously, they both occupied the same RAM.
Each program was written assuming it had the entire machine to itself.
Collisions were inevitable: one program could overwrite another's data, leading to crashes or corruption.
No protection existed: a single bug in a calculator app could corrupt the memory of the entire operating system, killing the whole machine.
External Fragmentation
Even if crashes were avoided, external fragmentation haunted the system. Consider a system with 64 KB of RAM:
Three programs (A, B, C) loaded side by side, each taking 20 KB. That leaves a 4 KB gap at the end.
When program B (in the middle) is closed, a 20 KB hole opens in the middle, plus the 4 KB at the end = 24 KB total free space.
Attempting to load a 22 KB program fails—even though 22 < 24—because the largest single contiguous gap is only 20 KB wide.
Programs at this time required a contiguous block of memory; they could not be split.
Memory was physically present but, because it was scattered, it was useless. The need for a new approach became clear.
Key takeaway: As long as software sees physical hardware directly, it is trapped by physical boundaries. To break free, the system must create an illusion where every program thinks it has the entire computer to itself.
Base and Bounds Registers: The First Translation Layer
In the late 1960s, hardware engineers introduced the Memory Management Unit (MMU) inside the CPU. The MMU performed a simple translation: it added a base register value to every memory address requested by a program.
Relocation with the Base Register
The operating system (OS) loads a program into RAM, say at physical address 30,000.
The OS stores
30,000in a special hardware register called the base register.The program is not told its real location; from its perspective, memory starts at address 0.
When the program reads data from address 0, the request passes through the MMU, which adds the base value (30,000), producing the physical address 30,000.
If the program accesses address 200, the MMU computes
200 + 30,000 = 30,200. This translation happens automatically for every memory access.
Protection with the Bound Register
A bound register stores the total amount of memory the program is allowed to use. For a program loaded at physical address 30,000 with 1000 bytes:
Base register = 30,000
Bound register = 1000
When the program tries to access virtual address 1500:
The MMU checks: Is 1500 < 1000? No.
The hardware immediately blocks the access and raises a segmentation fault (hardware exception).
Execution stops, control returns to the OS, which usually kills the offending program.
Segmentation fault: A hardware exception triggered when a program attempts to access memory outside its assigned region, enforcing memory protection directly by the hardware.
This was revolutionary: a broken application could no longer crash the entire machine.
The Crippling Limitation: Fragmentation Returns
Base and bounds still required every program to be stored as one contiguous chunk of physical memory. If a program needed 10 MB but free memory consisted of two 7.5 MB gaps, the system could not load it—even though total free space (15 MB) was sufficient.
To handle this, the OS performed compaction: stopping all execution, moving programs in RAM to close gaps. This was slow, wasted CPU power, and became noticeable as programs grew larger. Memory protection was solved, but the system was still a slave to the physical layout of RAM.
Segmentation: Logical Division of Programs
To break free from rigid contiguous blocks, engineers evolved base and bounds into segmentation. Instead of one base+bound pair, the hardware gave each program multiple pairs, one per logical segment.
How Segmentation Works
A program is divided into logical segments: code, heap, stack, each with its own entry in a segment table.
The OS can place each segment independently in RAM, wherever space fits—no longer forced into one block.
Each segment has its own base and bound values.
Advantages of Segmentation
Better memory utilization: The OS can scatter code, heap, and stack into separate gaps.
Permission control: The code segment can be marked read-only, preventing a buggy program from overwriting its own instructions.
Memory sharing: If ten instances of the same text editor run, the OS keeps one copy of the code segment in physical RAM; all ten programs point to the same physical memory for instructions while maintaining their own private data segments.
The Persistent Ghost: External Fragmentation
Because segments were still variable-sized, creating and destroying segments turned physical memory into a “Swiss cheese” of tiny unusable holes. The OS still spent significant time moving large segments around (compaction). The industry realized that the real problem was trying to fit variable-sized objects into a fixed-size hardware reality.
Key insight: The solution was to stop letting programs dictate the size of blocks—chop everything into identical, fixed-size pieces.
Paging: The Killer of External Fragmentation
Paging divides physical RAM into small, fixed-size slots called page frames (typically 4 KB). The program's virtual address space is also divided into identical fixed-size pieces called pages. Each virtual page can be placed in any physical page frame.
How Paging Eliminates External Fragmentation
Any page can go into any free frame, regardless of location.
No more “too small” gaps: every hole is exactly the size of a page frame.
Page 1 of a browser could be at the beginning of RAM, page 2 at the end, page 3 in the middle—the program sees a single unbroken line of memory.
The Page Table
The page table is the master map, managed by the OS. Every time the CPU wants to read a virtual address, it checks the page table to find the physical translation. For example: "Virtual page 5? That is currently sitting in physical frame 92."
The Trade-offs of Paging
Internal fragmentation: If a program needs only 1 byte, the system allocates a full 4 KB page. Thousands of tiny allocations waste space.
Latency penalty: Every memory access now requires two trips to RAM:
One to look up the page table (find where data is).
A second to actually fetch the data. Doubling memory access time would be unacceptable without a fix.
Definition: External fragmentation is eliminated because all holes are uniform sized. Internal fragmentation is the wasted space within a page when allocated memory is not fully used.
The TLB: Making Paging Fast
To solve the latency problem, hardware engineers added the Translation Lookaside Buffer (TLB) inside the CPU.
TLB: A Fast Address Cache
The TLB caches the most recent virtual-to-physical address translations.
When a program accesses memory, the CPU first checks the TLB.
TLB hit: Translation is in the cache, instant conversion, zero extra trips to RAM.
TLB miss: The system must perform a slow page table walk into RAM to find the translation.
Why the TLB Works So Well
Programs exhibit spatial locality and temporal locality—they tend to access nearby addresses repeatedly. Thanks to this behavior, the TLB hit rate exceeds 99% in practice, making virtual memory fast enough to be practical.
Key takeaway: The TLB is the tiny piece of silicon that makes the entire virtual memory abstraction feasible at modern speeds.
Demand Paging and Page Faults
The system needs to make memory feel truly unlimited. What happens when physical RAM is full? The final layer of illusion is demand paging.
Lazy Loading
When a program is launched, the OS does not load the entire program into RAM. It loads only the minimum needed to start.
The rest of the pages in the page table are marked with a special flag: not present.
The Page Fault Sequence
The program tries to access a not present page.
The CPU triggers a hardware exception called a page fault.
The OS pauses the program, reads the missing page from the SSD/hard drive into a free RAM slot.
The OS updates the page table (flips the flag to present) and tells the CPU to retry.
The program resumes, unaware that it was ever paused—it feels like the memory was always there.
This is how the OS transparently brings data into RAM only when needed.
When RAM Is Full: Eviction and Replacement Policy
If the RAM is completely full and a new page must be brought in from disk, the OS must evict an existing page. It selects a page to remove using a replacement policy, most commonly Least Recently Used (LRU). The OS finds pages that haven't been touched for a long time, writes them back to disk if modified, and then hands the now-empty frame to the requesting program.
LRU (Least Recently Used): A page replacement algorithm that selects the page that has not been accessed for the longest time, assuming it is least likely to be needed soon.
Thrashing: When the Illusion Shatters
The magic of virtual memory has a brutal breaking point. If too many demanding applications run simultaneously, the OS can enter a state called thrashing.
The Death Spiral
The system is so low on RAM that it constantly evicts pages that are immediately needed again—often within milliseconds.
The CPU spends nearly all its time (e.g., 99%) moving data between RAM and disk, and only 1% actually running user code.
The computer slows to a painful crawl: the cursor stutters, applications freeze, and the hard drive activity light remains solid (indicating constant swapping).
Why Thrashing Happens
Thrashing occurs when the working set of all running programs exceeds the physical RAM. The OS cannot satisfy memory demands, so it continuously swaps pages in and out, achieving almost no useful work.
Key takeaway: Thrashing represents the point where the virtual memory illusion collapses under the hard reality of physics. The system's actual performance degrades to near-zero.
Summary
The evolution of memory management demonstrates a masterpiece of cooperation between hardware and software:
Physical addressing: Fast but fragile, no protection, severe fragmentation.
Base and bounds: Introduced relocation and protection via MMU registers, but still required contiguous blocks and suffered external fragmentation.
Segmentation: Allowed multiple logical segments with independent placement and permissions, but variable sizes still caused external fragmentation.
Paging: Fixed-size pages and page frames eliminated external fragmentation entirely, at the cost of internal fragmentation and added latency.
TLB: A tiny cache that makes paging fast by storing recent translations; hit rates exceed 99%.
Demand paging + page faults: Allow the system to run programs larger than physical RAM by loading pages on demand and evicting others using replacement policies like LRU.
Thrashing: The system's breaking point, where constant swapping halts productive work.
When opening dozens of browser tabs without the computer complaining, these silent, invisible mechanisms are working at every memory access—translating addresses, caching translations, and gracefully (or not so gracefully) handling memory pressure.

Ask anything
We've read this whole study set, so questions about it get the best answers.