In the early days of computing, if you had 4GB of RAM, the absolute maximum size of a program you could run was 4GB. Virtual Memory completely changed this.
Virtual Memory is a technique that allows the execution of processes that are not completely in memory. One major advantage of this scheme is that programs can be substantially larger than physical memory. Furthermore, it abstracts main memory into a massive, uniform array of storage, separating logical memory as viewed by the user from physical memory.
Virtual memory is commonly implemented using Demand Paging.
Instead of loading an entire 50GB video game into RAM when you double-click the icon, a demand-paging system is lazy. It only loads pages into memory exactly when they are demanded during execution.
Because of demand paging, you can theoretically run a 100GB application on a machine with only 8GB of RAM. The OS will just constantly shuttle pages back and forth between the RAM and the hard disk.
What happens if a Page Fault occurs, but the physical RAM is completely full? There are no free frames available to load the required page from the disk.
The OS must perform Page Replacement:
Because disk I/O is incredibly slow, choosing the wrong victim frame (e.g., kicking out a page that the CPU is going to need again in 2 milliseconds) will cause severe performance degradation. This necessitates intelligent Page Replacement Algorithms, which we will cover in the next chapter.
The Swap File: Have you ever noticed a massive file on your hard drive called pagefile.sys (Windows) or a dedicated swap partition (Linux)? This is the backing store where the OS dumps all the pages that couldn't fit into physical RAM!