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:
- Larger address space than physical RAM.
- Memory protection between processes.
- Efficient process creation through techniques like copy-on-write.
- Simplified memory management for applications.
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 Address: The address generated by the CPU on behalf of a process.
- Page Table: A data structure managed by the OS that maps virtual page numbers to physical frame numbers. Each process has its own page table.
- Page: A fixed-size block of virtual memory.
- Frame: A fixed-size block of physical memory (RAM).
- Page Fault: An interrupt generated by the MMU when a process tries to access a virtual page that is not currently resident in physical memory.
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:
- First-In, First-Out (FIFO)
- Least Recently Used (LRU)
- Optimal (MIN) - theoretically best but impractical
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:
- System-wide Page Table
- Process Page Tables
- Working Set Lists: Tracks pages currently in use by a process.
- Standby List: Pages that are resident in memory but not actively used.
- Modified List: Pages that have been written to and need to be saved back to disk.
- Zeroed List: Available physical memory pages initialized to zero.