Virtual Memory Concepts

Virtual memory is a memory management technique implemented by the operating system (OS) that provides an "application view" of memory. This view is implemented in conjunction with the hardware (the Memory Management Unit, or MMU) on the CPU. Virtual memory enables the separation of user logical memory from physical memory. This separation has several benefits, including:

Address Translation

When a process accesses a virtual address, the Memory Management Unit (MMU) translates it into a physical address. This translation process involves the following key components:

Virtual Memory Address Translation Diagram
Conceptual diagram of virtual to physical address translation.

The Translation Process

The MMU uses the virtual address to look up the corresponding physical address in the process's page table. If the page is present in memory (indicated by a valid bit in the page table entry), the MMU uses the physical frame number to construct the physical address. If the page is not present, a page fault occurs, and the OS takes over to handle the situation.

Note: The page table itself is stored in physical memory. To speed up the translation process, a Translation Lookaside Buffer (TLB) is used to cache recent virtual-to-physical address translations.

Paging and Demand Paging

Virtual memory systems typically use a technique called paging. In paging, a process's address space is divided into pages, and physical memory is divided into frames of the same size. When a page is needed by a process but is not in physical memory, it can be retrieved from secondary storage (like a hard drive or SSD) and loaded into an available frame. This mechanism is often referred to as demand paging, meaning pages are loaded only when they are actually needed.

Page Replacement Algorithms

When memory is full and a new page needs to be brought in, the OS must decide which existing page to replace. Various page replacement algorithms are used, such as:

Windows uses sophisticated algorithms, often variations of LRU, to optimize performance.

Tip: Understanding page faults and page replacement is crucial for diagnosing performance issues related to memory pressure.

Memory-Mapped Files

Virtual memory also facilitates memory-mapped files. This technique allows a file on disk to be mapped directly into a process's virtual address space. Operations on the memory region then automatically interact with the file, simplifying file I/O and enabling efficient sharing of file data between processes.

Memory Protection

Each page table entry typically includes protection bits that control access permissions (e.g., read, write, execute) for that page. The MMU enforces these permissions, preventing a process from accessing memory it does not own or from performing unauthorized operations.

System Memory Structures

Key OS structures involved in virtual memory management include: