TutorialsOperating Systems
Core CS

Virtual Memory

Virtual Memory is a memory management technique that creates an illusion of a very large main memory by combining physical RAM with secondary storage (like an SSD or hard drive). It allows a computer to execute programs larger than its installed RAM by loading only active chunks of code into memory while temporarily parking idle data on the disk.

<h2><!--StartFragment--><p><b>Definition &amp; Key Points</b>&nbsp;</p><p>Virtual memory maps the virtual addresses used by an application onto physical addresses in computer memory. The operating system handles this translation automatically via dedicated hardware called the <b>Memory Management Unit (MMU)</b>.</p><p><!--StartFragment--><!--EndFragment--></p><ul><li><!--StartFragment--><img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTi6Yrl7gH_nsRNnIguzPcU-YDNXIRxySnTOfUUbG-u4ubT-wUt_VccDPp2&amp;s=10" alt="Virtual memory mapping via page table, AI generated"><!--EndFragment--><p><b>Pages and Frames:</b> Virtual address space is divided into fixed-size blocks called <b>pages</b>, while physical RAM is divided into matching blocks called <b>page frames</b>.</p></li><li><p><b>Page Table:</b> A data structure maintained by the OS to map each virtual page to its corresponding physical frame or secondary storage location.</p></li><li><p><b>Demand Paging:</b> Pages are loaded into physical RAM only when the program actually needs them, rather than loading the entire application at launch.</p></li><li><p><b>Page Fault:</b> An interrupt raised by hardware when a program accesses a page that is not currently in physical RAM, triggering the OS to fetch it from disk.</p></li><li><p><b>Swapping / Paging Out:</b> When RAM is full, the OS moves inactive pages to a designated swap space on secondary storage to free up memory for active tasks.</p></li></ul><p><b>Significance</b></p><ul><li><p><b>Removes Hardware Limits:</b> Applications can run seamlessly even if their memory footprint exceeds installed physical RAM.</p></li><li><p><b>Process Isolation &amp; Protection:</b> Every process operates in its own isolated virtual address space, preventing buggy or malicious software from reading or overwriting another process's memory.</p></li><li><p><b>Higher Multiprogramming:</b> By only keeping active parts of multiple programs in RAM, the CPU can juggle significantly more tasks simultaneously.</p></li></ul><p><b>Advantages</b></p><ul><li><p><b>Cost-Efficient Capacity:</b> Expands usable working memory using inexpensive disk storage without requiring physical RAM upgrades.</p></li><li><p><b>Memory Protection:</b> Enhances system stability and security through strict per-process memory boundaries.</p></li><li><p><b>Simplified Programming:</b> Developers do not need to manually manage memory overlays or worry about physical RAM constraints when writing software.</p></li><li><p><b>Efficient Memory Sharing:</b> Common libraries (e.g., standard C runtime or shared DLLs) can be mapped once into physical RAM and shared across multiple processes.</p></li></ul><p><b>Disadvantages</b></p><ul><li><p><b>Performance Overhead:</b> Address translation adds CPU cycles, though hardware caches like the Translation Lookaside Buffer (TLB) help minimize the latency.</p></li><li><p><b>Risk of Thrashing:</b> If physical RAM is critically low, the system spends more time swapping pages back and forth to the drive than executing instructions, causing extreme lag.</p></li><li><p><b>Disk Wear and Space Consumption:</b> Frequent swapping consumes storage drive space and can increase write cycles on solid-state drives (SSDs).</p></li><li><p><b>Implementation Complexity:</b> Requires complex operating system algorithms for page replacement (such as LRU or FIFO) and specialized hardware support<span style="color: rgb(0, 0, 0);"><b>.</b></span></p></li></ul></h2>