Introduction
Virtual memory is an abstraction that separates the logical (virtual) address space seen by a process from the physical memory actually installed in the machine. It lets a process behave as though it has access to a large, contiguous, private address space -- often far larger than the amount of physical RAM present -- while the OS and hardware transparently keep only the actively used portions in RAM and the rest on secondary storage (disk/SSD).
Cricket analogy: Virtual memory is like a stadium selling more seats on paper than the physical stand can hold at once, because not every ticket holder shows up simultaneously, the OS manages who's actually 'in the seat' (RAM) at any moment.
Explanation
Virtual memory is possible because most programs do not use their entire address space at once, and even the parts they do use are not needed all at the same time (the principle of locality: programs favor a relatively small set of pages during any given phase of execution). This means the OS does not have to keep an entire process resident in physical memory to run it; it only needs to keep the pages currently being referenced. The mechanism that implements this is demand paging (covered in depth in a later topic): pages are loaded into physical frames only when referenced for the first time, and each page table entry has a valid/invalid bit indicating whether that page currently has a frame in RAM. If a process accesses a page whose bit is invalid, the hardware raises a page fault, trapping into the OS, which locates the page on disk (in the process's backing store or swap space), finds or frees a physical frame, loads the page in, updates the page table, and resumes the instruction. Virtual memory also enables benefits beyond just running programs bigger than RAM: it allows more processes to be kept partially resident simultaneously (improving CPU utilization and throughput), lets processes share memory (such as shared libraries) via common page mappings, and simplifies program creation since programmers do not need to worry about the physical memory size of the target machine.
Cricket analogy: Just as a batsman only needs to focus on the current bowler's line and length rather than memorizing the entire opposition's playing history at once, demand paging loads only the pages a process is actively referencing, not the whole program.
Example
/* Conceptual illustration: a process's virtual address space is much larger */
/* than physical RAM, and only referenced pages are actually resident. */
#include <stdio.h>
#define VIRTUAL_SPACE_GB 64 /* e.g. a 64 GB virtual address space */
#define PHYSICAL_RAM_GB 8 /* only 8 GB of physical RAM installed */
#define PAGE_SIZE_KB 4
typedef struct {
int valid; /* 1 = page currently resident in RAM */
unsigned int frame; /* meaningful only if valid == 1 */
} PTE;
void access_page(PTE *page_table, unsigned int page_number) {
if (page_table[page_number].valid) {
printf("Page %u resident at frame %u (no fault)\n",
page_number, page_table[page_number].frame);
} else {
printf("Page %u NOT resident -> page fault -> OS loads it from disk\n",
page_number);
/* OS would: find a free frame (or evict one), read the page from */
/* the backing store into that frame, then set valid = 1. */
page_table[page_number].valid = 1;
page_table[page_number].frame = 123; /* frame chosen by OS */
}
}
int main(void) {
PTE page_table[16384] = {0}; /* all pages start out non-resident */
printf("Virtual space: %d GB, Physical RAM: %d GB\n",
VIRTUAL_SPACE_GB, PHYSICAL_RAM_GB);
access_page(page_table, 500); /* first touch -> page fault */
access_page(page_table, 500); /* second touch -> already resident */
return 0;
}Analysis
Notice the address space (64 GB) is deliberately larger than the installed RAM (8 GB); this is exactly what virtual memory permits, because at any instant only a small working set of pages needs to be resident. The first access to page 500 finds valid == 0, so a page fault occurs and the OS brings the page in from the backing store, updating the page table entry. The second access to the same page finds valid == 1 and proceeds without any fault, since the page is already resident. This valid/invalid bit mechanism, combined with the OS's fault handler, is precisely the demand-paging machinery that makes the 'infinite virtual, limited physical' illusion work.
Cricket analogy: The 64GB address space versus 8GB RAM is like a stadium's total seating capacity being far bigger than the currently occupied section; the first fan checking seat 500 finds it empty (fault) and gets seated, the second check finds them already seated (no fault).
Key Takeaways
- Virtual memory decouples a process's logical address space from physical RAM, allowing the logical space to be larger than physical memory.
- The principle of locality justifies keeping only actively used pages resident, since programs reference a small working set at any given time.
- Demand paging is the mechanism: pages load into RAM only when first referenced, tracked by a valid/invalid bit per page table entry.
- A reference to an invalid page triggers a page fault, which traps to the OS to locate/load the page from the backing store and update the page table.
- Beyond running large programs, virtual memory improves multiprogramming degree, enables memory sharing (e.g., shared libraries), and frees programmers from worrying about physical memory limits.
Practice what you learned
1. What core principle allows virtual memory to work even though physical RAM is much smaller than the virtual address space?
2. What mechanism actually implements the virtual memory abstraction by loading pages into RAM only when first referenced?
3. What happens when a process accesses a page whose page table entry has its valid bit set to invalid?
4. Which of the following is NOT a benefit typically attributed to virtual memory?
Was this page helpful?
You May Also Like
Paging
Splitting logical and physical memory into fixed-size pages and frames to eliminate external fragmentation.
Demand Paging
Lazy loading of pages into memory only when referenced, and the step-by-step page-fault handling sequence that makes it work.
Page Replacement Algorithms
How an OS decides which page to evict on a page fault when all frames are full, compared via FIFO, LRU, and Optimal.