What is Virtual Memory?
Imagine you have a small study desk but a huge bookshelf full of books. You can’t fit all the books on the desk at once, right?
So, you keep only the ones you need right now on your desk (fast and easy to reach) and keep the rest on the bookshelf (takes longer to get).
That’s exactly how virtual memory works.
Your RAM (main memory) is the small, fast desk.
Your hard disk (or SSD) is the big, slower bookshelf.
Virtual memory gives your computer the illusion that it has more RAM than it actually does by temporarily storing parts of programs on the disk and bringing them to RAM only when needed.
💡 Why Virtual Memory?
Sometimes, programs are larger than the available RAM.
Instead of saying “sorry, I can’t run this big program,” the system uses virtual memory to make it fit by loading only the active parts.
It’s like saying:
“I’ll only keep the chapters I’m currently studying on the desk, and swap them out when I need new ones.”
📦 The Concept of Paging
Now, here’s where paging comes in — the method that makes virtual memory work smoothly.
Think of paging as dividing both your virtual memory and physical memory into small, fixed-size chunks:
- In virtual memory, these chunks are called pages.
- In physical memory (RAM), these chunks are called frames.
Usually, a page and a frame are the same size — for example, 4 KB each.
🧩 Basic Idea:
When a program is running, its pages (from virtual memory) are mapped to available frames (in RAM).
Only the pages currently needed by the CPU are kept in RAM.
If a page isn’t in RAM, it’s fetched from the disk — this is called a page fault.
⚙️ How Paging Works — Step by Step
Let’s go through this slowly, like a small story.
- Program divided into pages:
The operating system divides the program’s address space into pages (say, Page 0, Page 1, Page 2, …). - RAM divided into frames:
The physical memory is divided into equal-sized frames (Frame 0, Frame 1, Frame 2, …). - Mapping using Page Table:
Each process has a page table that keeps track of where each virtual page is located in physical memory. - CPU generates a virtual address:
When the CPU wants data, it gives a virtual address.
The page number part of the address is used to look up the frame number in the page table. - Translation to physical address:
Once the frame number is found, the system forms the physical address and accesses the data from that frame. - If the page is not in memory (page fault):
- The OS pauses the program.
- Brings the required page from disk to an empty frame in RAM.
- Updates the page table.
- Resumes the program.
🧾 Example
Let’s take a simple example:
| Virtual Page | Frame Number |
|---|---|
| Page 0 | Frame 2 |
| Page 1 | Frame 5 |
| Page 2 | Frame 1 |
| Page 3 | (Not in memory) |
Now, if the CPU wants data from Page 1, the system looks into the page table → finds it’s in Frame 5 → directly goes there.
But if it wants data from Page 3, the page isn’t in memory.
So, a page fault happens — the OS fetches Page 3 from disk into an empty frame, say Frame 0.
🧮 Structure of a Virtual Address
A virtual address is divided into two parts:
Virtual Address = Page Number + Offset
- Page Number: identifies which page the address belongs to.
- Offset: tells how far into that page the actual data is.
For example, if a virtual address is 10011010, and the system uses 4-bit page numbers:
- Page Number =
1001(Page 9) - Offset =
1010(10th byte in that page)
🧭 Diagram – Paging in Action
+--------------------+
| CPU |
+--------+-----------+
|
Virtual Address (Page No + Offset)
|
+-------v--------+
| Page Table |
+-------+--------+
|
Finds Frame Number
|
+-------v--------+
| Main Memory |
+-------+--------+
|
Access data from Frame
⚖️ Advantages of Paging
✅ Efficient Memory Use:
Only the required pages are kept in memory — no wasted space.
✅ Program Can Be Larger Than RAM:
You can run programs bigger than your physical memory.
✅ No External Fragmentation:
Because all pages and frames are the same size, memory fits perfectly like tiles on a floor.
⚠️ Disadvantages
❌ Page Table Overhead:
Each process needs a page table, which takes space.
Large programs → large tables.
❌ Page Fault Delay:
Fetching pages from disk is slow, so page faults can reduce speed.
❌ Internal Fragmentation:
A little space inside each frame might go unused if the page isn’t completely full.
💡 Real-Life Analogy Recap
Let’s bring it all together with our study desk analogy again:
- Virtual memory = Your entire bookshelf.
- Physical memory (RAM) = Your desk.
- Pages = Individual chapters of books.
- Frames = Spaces on your desk where chapters fit.
- Page table = The index showing which chapter is currently on which part of the desk.
- Page fault = When you need a chapter that’s still on the bookshelf.
Simple, right? 😊
🧠 In a Nutshell
| Concept | Description |
|---|---|
| Virtual Memory | Creates an illusion of a large memory using disk space. |
| Paging | Divides memory into fixed-size pages/frames for easier management. |
| Page Table | Maps virtual pages to physical frames. |
| Page Fault | When a required page isn’t in memory, it’s brought from disk. |