Virtual Memory (VM) is a memory management technique that provides an abstraction of main memory, offering each process a consistent and isolated address space, which can be significantly larger than the actual physical RAM. By treating disk storage as an extension of physical memory, VM systems create a logical memory space larger than the available RAM and provide each process with a protected memory environment, ensuring one process cannot directly access another’s memory, thereby enhancing system stability and security.
What Problems Does Virtual Memory Solve?
- Expands Usable Memory Space: Even with limited physical memory, VM allows operating systems and applications to use more memory space than the capacity of the physical RAM.
- Provides Memory Isolation: Each process has its own virtual address space, preventing memory conflicts between different processes.
- Simplifies Memory Management: It simplifies program development by abstracting away the details of physical memory allocation and supports simpler linking and loading processes.
Memory Management Unit — Page
To efficiently manage VM, memory is divided into fixed-size blocks called “pages.” Similarly, physical memory is partitioned into matching “page frames” or “physical pages.” When a program requests access to a specific virtual address, the OS checks if the page containing this address resides in physical memory. If it does, direct access occurs; otherwise, the page must be loaded from disk into physical memory, a process known as “page replacement.”
Virtual Addressing and Address Translation
The conversion from virtual addresses to physical addresses is essential for accessing data. This translation is implemented in hardware by the MMU (Memory Management Unit), which uses page tables to record the mapping between virtual pages and physical pages. Each time the CPU generates a virtual address, the MMU looks up the page table to determine the corresponding physical address.
- Page Hit: If the required page is already in physical memory, the lookup succeeds, termed a page hit.
- Page Fault: If the required page is not in physical memory, a page fault occurs, and the OS must load the page from disk into physical memory.
- TLB (Translation Lookaside Buffer): To speed up address translation, the MMU typically includes a TLB that caches recently used page table entries, allowing for faster address translation without accessing the main memory page table.
Multi-Level Page Tables
For systems with large address spaces, such as 64-bit architectures, single-level page tables can become excessively large, consuming significant physical memory. Therefore, multi-level page tables are designed to reduce the demand on physical memory for page tables. Multi-level page tables divide the page table itself into multiple layers, creating entries only when a particular virtual address is used. This approach significantly reduces wasted space due to unused address regions.
In summary, the virtual memory mechanism not only increases the flexibility and efficiency of memory usage but also enhances the stability and security of operating systems, making it an indispensable part of modern computing systems.