[3.3.4] Virtual memory
Virtual Memory: What it is, how it works, and why computers need it
Big idea
Modern computer systems often need to run more applications and open more files than there is space for in Random Access Memory (RAM). Virtual memory is a technique used by the operating system (OS) to make main memory appear larger than it really is by temporarily moving blocks of data between RAM and secondary storage (usually a hard disk drive or solid-state drive). This allows a computer to keep more programmes and data apparently in memory, even when physical RAM is full.
At IGCSE level, you should be able to define virtual memory, describe the basic mechanism of paging and swapping, explain how and why it is created, and discuss the benefits and limitations of using it. You should also recognise common real-world situations where virtual memory is helpful and where it can become a problem for performance.
Key definitions
- Virtual memory (VM): A memory management technique that uses secondary storage to extend the apparent capacity of main memory (RAM).
- Page: A fixed-size block of virtual memory. Typical page sizes are 4 KiB, 8 KiB or larger. For example, 4 KiB equals
0x1000bytes in hexadecimal. - Frame: A block of physical RAM that can hold a page.
- Paging: Moving pages between RAM and secondary storage to ensure the currently needed pages are in RAM.
- Page table: A data structure that maps virtual addresses used by programmes to physical addresses in RAM (or marks them as stored on disk).
- Swap file / page file: The file on secondary storage used to hold pages that have been moved out of RAM. Some systems use a swap partition instead of a file.
- Thrashing: A severe slowdown that happens when the OS spends most of its time swapping pages in and out rather than executing programme code.
Why virtual memory is necessary
RAM is fast but expensive compared with secondary storage. It is also limited in size on many devices. When several applications are open, the total memory required can exceed the physical RAM available. Without virtual memory, the OS would have to refuse to open new applications or would crash running programmes. With virtual memory, the OS can keep the most active parts of programmes in RAM while less active parts are stored temporarily on disk, ready to be brought back when needed.
Virtual memory also improves isolation and security. Each process can be given its own virtual address space, meaning that the addresses it sees (e.g. 0x00402000) are mapped by the OS to wherever the data actually resides. This prevents one process from directly reading or corrupting another process’s memory.
How virtual memory is created and used
The paging mechanism
When a programme runs, it uses virtual addresses. The OS and the hardware Memory Management Unit (MMU) translate these to physical addresses in RAM using the page table. RAM is divided into frames that can each hold one page. If a programme tries to access a page that is not currently in RAM, a page fault occurs. The OS then finds a free frame in RAM (or frees one by moving a different page out to disk), loads the required page from the swap file/partition, updates the page table, and resumes the programme.
Choosing which page to move
To keep performance reasonable, the OS chooses pages to move out of RAM that are unlikely to be needed soon. Simple strategies include FIFO (first-in-first-out) or more advanced approaches approximating Least Recently Used (LRU), which guesses that a page not used recently is a better candidate to swap out.
Typical workflow
- Programme requests data at a virtual address.
- MMU consults the page table to translate to a physical address.
- If the page is present in RAM, the access continues quickly.
- If not in RAM (a page fault), the OS:
- Chooses a victim page in RAM (based on a policy).
- Writes the victim page to the swap file if it has changed (dirty page) or discards it if unchanged.
- Loads the required page from swap into the freed frame.
- Updates the page table so future accesses hit in RAM.
Scenarios in practice
To see how virtual memory behaves, compare these situations. Notice how the OS adapts, and when performance risks appear.
You have a browser, a music player and a word processor open. RAM is nearly full, but only small parts of each programme are active at once. The OS occasionally moves idle pages (e.g. rarely used plug-in code) to the swap file. Page faults happen infrequently, and you hardly notice any delay. Virtual memory allows comfortable multitasking without needing to close applications.
You open many large tabs, a video editor and a game. RAM fills quickly. As you switch applications, the OS must reload pages for the app you are returning to, causing more frequent page faults. You notice short pauses when alt-tabbing, saving, or loading media. Work is still possible, but responsiveness drops.
You try to render a video while running multiple memory-hungry apps on a system with little RAM. Almost every action triggers a page fault, and the OS repeatedly swaps the same pages in and out. This is thrashing. The disk light may stay on and everything feels extremely slow. The solution is to reduce workload, add more RAM, or adjust application behaviour.
Where the swap lives: file vs partition
Operating systems store swapped-out pages either in a dedicated swap partition or inside a normal swap/page file. A partition provides a reserved area of the disk that cannot be used for anything else, whereas a file is easier to resize or move. The exact choice is OS-dependent and can be changed by system administrators.
| Aspect | Swap file (pagefile) | Swap partition |
|---|---|---|
| Flexibility | Easy to resize or disable within the filesystem | Fixed size unless the disk is repartitioned |
| Setup | Simpler to configure after installation | Often set during OS installation |
| Performance | Comparable on modern systems; depends on disk speed | Can be slightly more predictable in some setups |
| Portability | Moves with the filesystem image | Tied to a particular partition layout |
Advantages and limitations of virtual memory
Advantages
- Runs more applications: Lets users open larger programmes or more windows than physical RAM alone would allow.
- Stability: Reduces crashes caused by low memory by providing a safety net.
- Process isolation: Virtual address spaces protect programmes from each other.
- Efficient memory use: Only the working set of each programme needs to remain in fast RAM.
Limitations
- Performance penalty: Disk or SSD access is far slower than RAM. Excessive paging causes lag.
- Thrashing risk: If workload is too large for RAM, the system can spend most time swapping.
- Storage wear: On SSDs, heavy swapping contributes to write wear over time (modern SSDs manage this well, but it still exists).
Virtual addresses and the MMU (conceptual view)
Each process uses virtual addresses that make memory look continuous, even if physically it is scattered. For example, a programme might store data starting at virtual address 0x20000000. The MMU, with the OS, maps this to any available frames in RAM. If a referenced page is on disk, the OS handles the page fault, loads it into a free frame, and updates the mapping before the programme continues. This all happens without the programme needing to know the details.
Managing performance: best practices
- Add more RAM: The most direct way to reduce paging and prevent thrashing.
- Close unused applications: Frees RAM so the OS swaps less often.
- Use lighter alternatives: Choose software that uses less memory where possible.
- Keep sufficient free disk space: The swap file/partition needs space to grow when demand increases.
- Understand workload: Tasks such as video editing, large spreadsheets, or virtual machines are memory-hungry; plan accordingly.
Deep Dive: Paging vs segmentation (awareness)
Some historical systems used or combined segmentation with paging. Segmentation divides memory into variable-sized logical segments (e.g. code, data, stack). Paging divides memory into fixed-size pages. Most general-purpose systems today rely on paging, because fixed sizes simplify allocation and reduce external fragmentation. At IGCSE, focus on paging as the standard approach underpinning virtual memory.
Summary table: RAM, secondary storage and virtual memory
| Property | RAM (Primary storage) | Secondary storage (e.g. SSD/HDD) | Virtual memory |
|---|---|---|---|
| Speed | Very fast (nanoseconds) | Much slower than RAM | Depends on secondary storage speed; slower than RAM |
| Volatility | Volatile (clears when power off) | Non-volatile | Non-volatile backing store, but used to extend volatile RAM |
| Purpose | Holds running programmes and data | Long-term storage of files and OS | Extends RAM capacity temporarily for active processes |
| Managed by | Hardware and OS | Filesystem and disk firmware | OS memory manager and MMU |
Key Takeaways
- Virtual memory lets computers run more or larger programmes than physical RAM alone by moving pages between RAM and secondary storage.
- Pages and frames are fixed-size blocks; a page table and the MMU map virtual addresses to physical locations.
- A page fault triggers the OS to fetch the required page from the swap file/partition into RAM.
- Virtual memory improves stability and isolation but can hurt performance if paging is excessive, leading to thrashing.
- Adding RAM, closing unused apps and ensuring adequate free disk space help reduce reliance on virtual memory.
- Typical page sizes are a few KiB (e.g.
0x1000bytes), but exact values vary by system.