Virtual Memory Fundamentals
We have established that Virtual Memory is a software trick to make programs believe they have more RAM than physically exists. But how does an operating system actually execute this trick?
The answer, for almost every modern operating system, is a technique called Demand Paging.
What is Demand Paging?
In the past, when you clicked an application to open it, the OS would copy the entire application from the hard drive into RAM. If the application was 2 GB and you only had 1 GB of free RAM, it simply would not open.
Demand Paging changes this. When an application starts, the OS only loads the bare minimum "pages" required to begin execution. The rest of the application remains on the hard disk. The OS only fetches additional pages "on demand"—exactly when the CPU explicitly asks for them.
The Core Components
- Page: A fixed-size block of logical memory belonging to the program (typically 4 KB).
- Frame: A fixed-size block of physical RAM. Frames are exactly the same size as Pages.
- Swap Space: A dedicated, hidden section of the hard disk used by the OS as an "overflow" area to store pages when the physical RAM gets full.
The Valid / Invalid Bit
Because some pages are in RAM and some are sitting on the disk, the Memory Management Unit (MMU) needs a fast way to tell the difference. It does this by adding an extra bit to every entry in the Page Table: the Valid/Invalid bit.
If the bit is marked "Valid" (v), it means the page is currently loaded in a physical RAM frame. If it is marked "Invalid" (i), it means the page is legally part of the program, but it is currently sitting on the hard disk.
The Page Fault Workflow
What happens when the CPU executes an instruction that tries to access an "Invalid" page? The hardware triggers a critical interrupt called a Page Fault. Here is exactly what the OS does to resolve it:
- Trap: The MMU notices the Invalid bit and traps to the Operating System.
- Verify: The OS checks its internal tables to ensure the memory request is legal. (If the program was trying to access memory it doesn't own, the OS kills it).
- Find Space: The OS looks for a free physical Frame in RAM.
- Fetch: The OS issues an I/O command to fetch the required Page from the hard disk and copy it into the free Frame.
- Update Table: The OS updates the Page Table, setting the frame number and flipping the bit to "Valid" (v).
- Restart: The OS restarts the exact CPU instruction that caused the trap. This time, the memory is in RAM, and the instruction succeeds instantly.
Summary
Demand paging is the engine of virtual memory. By combining the fixed-size efficiency of Paging with the clever "lazy loading" of Demand fetching, the OS successfully fools programs into thinking they have unlimited RAM, loading only what is strictly necessary at any given nanosecond.
