Learn how virtual memory works in operating systems with simple explanations and practical code examples. Ideal for students and beginners.
Operating systems manage hardware and software resources, and one of their most important roles is memory management. A key concept here is Virtual Memory. Let's explore what it is and how it works in a beginner-friendly manner.
Virtual memory is a memory management technique used by operating systems to provide the illusion of a large, continuous memory space to applications, even if the physical RAM is smaller.
Imagine your system has 4GB of RAM, but you’re running programs that need 6GB. Virtual memory lets your system "pretend" it has 6GB by using part of the hard disk (usually called the swap file or paging file) as additional memory.
To run programs larger than physical RAM.
To isolate processes (each process gets its own virtual address space).
To allow multitasking.
To improve security and stability.
Virtual memory involves several concepts:
Paging is a technique where memory is divided into fixed-size blocks:
Pages (in virtual memory)
Frames (in physical memory)
The OS keeps a page table that maps virtual pages to physical frames.
Virtual Address → Page Table → Physical Address
Let’s understand this with a simple code example in C:
#include <stdio.h>
#define FRAME_SIZE 4
#define NUM_PAGES 4
#define NUM_FRAMES 3
int page_table[NUM_PAGES];
int memory[NUM_FRAMES];
void initialize() {
for (int i = 0; i < NUM_PAGES; i++)
page_table[i] = -1; // -1 means not loaded
for (int i = 0; i < NUM_FRAMES; i++)
memory[i] = -1; // -1 means frame is empty
}
void load_page(int page_number) {
int free_frame = -1;
// Find an empty frame
for (int i = 0; i < NUM_FRAMES; i++) {
if (memory[i] == -1) {
free_frame = i;
break;
}
}
if (free_frame == -1) {
printf("No free frames, using FIFO to replace page 0.\n");
free_frame = 0; // FIFO replacement
}
memory[free_frame] = page_number;
page_table[page_number] = free_frame;
printf("Page %d loaded into frame %d.\n", page_number, free_frame);
}
int main() {
initialize();
load_page(0);
load_page(1);
load_page(2);
load_page(3); // This will trigger replacement
printf("\nPage Table:\n");
for (int i = 0; i < NUM_PAGES; i++) {
printf("Page %d → Frame %d\n", i, page_table[i]);
}
return 0;
}
page_table
keeps track of where each page is loaded.
memory
simulates physical frames.
If no free frame is available, a page is replaced using FIFO (First-In-First-Out).
The CPU generates a virtual address. The OS translates it to a physical address using the page table.
Virtual Address = Page Number + Offset
Physical Address = Frame Number + Offset
This is done automatically by the Memory Management Unit (MMU) in hardware.
In real-world OSes, not all pages are loaded into memory at once. Pages are loaded only when needed (on demand).
If a page is not in memory, a page fault occurs, and the OS loads the required page from disk.
Benefit | Description |
---|---|
Efficient Use of RAM | Only needed pages are kept in memory |
Multitasking | Each process gets its own memory space |
Security | Isolation between processes |
Flexibility | Can run large applications on limited memory |
Drawback | Description |
---|---|
Slower than RAM | Accessing disk is slower than accessing RAM |
Thrashing | Too many page faults can degrade performance |
When you open many browser tabs or apps, some may be moved to disk (swap) temporarily. When you return to them, there might be a delay — this is the OS retrieving them from virtual memory.
Page Table: Keeps mapping from virtual to physical addresses
Paging: Breaks memory into equal parts (pages and frames)
Swap File: Disk space used as an extension of RAM
Page Fault: When the required page is not in memory
MMU: Hardware that performs address translation
Virtual memory is a brilliant abstraction that allows computers to run large programs efficiently, manage multiple processes, and keep systems stable and secure. With the help of paging, page tables, and swapping, virtual memory bridges the gap between limited physical memory and the growing demands of modern software.
Thank you for visiting! Enjoy exploring our diverse collection of blogs, crafted with passion and insight to inspire and inform. Happy reading!