Introduction
A computer program often requires more memory than the physical RAM installed in a machine. This constraint would halt execution if every byte had to reside in physical memory simultaneously. Virtual memory resolves this limitation by creating an abstraction layer between the addresses a program uses and the actual hardware addresses. This article provides an authoritative explanation of virtual memory, covering the distinction between virtual and physical addresses, the role of pages and page tables, and the function of the Translation Lookaside Buffer (TLB). You will gain an understanding of how modern operating systems and processors collaborate to provide processes with a large, isolated, and protected address space. The concepts of page faults and the relationship between virtual memory and CPU caches are also examined.
(toc) #title=(Table of Content)
What Is Virtual Memory?
Virtual memory is a memory-management technique that allows a program to use a virtual address space larger than the available physical RAM. The operating system does not need to keep the entire program in memory at once. Instead, it keeps only the currently needed portions in RAM and stores the remainder on secondary storage, such as an SSD or HDD.
This provides the program with the illusion of a large, continuous memory space. For example, a system with 8 GB of physical RAM can run a program that requires a 16 GB virtual address space. The OS manages this by loading pages on demand.
Virtual Address vs. Physical Address
The address generated by the CPU for a program is called a virtual address. The actual address in the RAM hardware is called a physical address. The translation from virtual to physical is performed by a combination of hardware and the operating system.
\[ \text{Virtual Address} \xrightarrow{\text{Address Translation}} \text{Physical Address} \]
This separation is fundamental to process isolation and memory protection.
Pages and Frames
Virtual memory is divided into fixed-size blocks called pages. Physical memory is divided into blocks of the same size called frames.
A key insight is that a virtual page does not need to map to a physical frame with the same number. The operating system can place any virtual page into any available physical frame. For instance, Virtual Page 4 might be stored in Physical Frame 12, while Virtual Page 2 resides in Physical Frame 5.
| Virtual Page | Physical Frame |
|---|---|
| 0 | 3 |
| 1 | 8 |
| 2 | 5 |
| 3 | 1 |
| 4 | 12 |
The Page Table
The mapping between virtual pages and physical frames is stored in a data structure called a page table. The CPU uses the page table to translate a virtual page number (VPN) into a physical frame number (PFN).
Virtual Address Structure
A virtual address is composed of two parts: the Virtual Page Number and the Page Offset.
\[ \text{Virtual Address} = \text{Virtual Page Number} + \text{Page Offset} \]
Consider a system with a 32-bit virtual address and a page size of 4 KB. Since \(4\text{KB} = 2^{12}\), the page offset requires 12 bits. The remaining 20 bits constitute the Virtual Page Number.
During translation, the page table translates the VPN to a PFN. The page offset remains unchanged.
\[ \text{Physical Address} = \text{Physical Frame Number} + \text{Same Page Offset} \]
Address Translation and the TLB
Every memory access would require two accesses if the page table were consulted directly: one to fetch the translation and another to fetch the data. To eliminate this overhead, processors include a Translation Lookaside Buffer (TLB) .
The TLB is a small, fast cache that stores recently used virtual-to-physical address translations.
TLB Hit vs. TLB Miss
- TLB Hit: The required translation is found in the TLB. The physical address is obtained quickly.
- TLB Miss: The translation is not in the TLB. The processor must consult the page table, obtain the mapping, and update the TLB.
A TLB miss is not a page fault. It simply means the translation was not cached.
Page Faults
A page fault occurs when the required virtual page is not currently present in physical RAM. The page table indicates that the page is not present, triggering a fault. The operating system must then retrieve the page from secondary storage, load it into RAM, update the page table, and resume the program.
A page fault is significantly more expensive than a TLB miss or a cache miss.
Virtual Memory and CPU Cache
The complete memory access path involves both the TLB and the CPU cache. Conceptually, the process flows as follows:
- CPU generates a virtual address.
- TLB is checked for a translation.
- Physical address is formed.
- Cache is checked for the data.
- If a cache miss occurs, DRAM is accessed.
- If the page is not in DRAM, a page fault occurs, and the OS retrieves it from storage.
Benefits of Virtual Memory
Virtual memory provides several critical benefits:
- Larger address space: Programs can use more memory than physically available.
- Process isolation: Each process has its own virtual address space, preventing direct interference.
- Efficient RAM utilization: Only actively needed pages reside in RAM.
- Protection: Page-table permissions can enforce read, write, and execute restrictions.
Important Terms to Remember
| Term | Meaning |
|---|---|
| Virtual Address | Address generated by the CPU/program |
| Physical Address | Actual address in physical memory |
| Page | Fixed-size block of virtual memory |
| Frame | Fixed-size block of physical memory |
| Page Table | Maps virtual pages to physical frames |
| TLB | Fast cache of recent address translations |
| Page Fault | Required page is not currently in RAM |
Conclusion
Virtual memory is a cornerstone of modern computing, enabling efficient multitasking, process isolation, and the execution of large applications. The collaboration between the operating system, the memory management unit (MMU), and the TLB ensures that programs operate within a protected and expansive address space. Understanding the distinction between a cache miss, a TLB miss, and a page fault is essential for diagnosing performance issues and designing efficient software.