1. Introduction
Modern computing systems must support multiple processes, large applications, and efficient resource utilization despite having limited physical memory (RAM).
This creates a fundamental challenge:
How can a system execute programs whose size exceeds the available physical memory?
The solution is Virtual Memory, one of the most important concepts in operating systems.
Virtual memory allows each process to operate as if it has access to a large, continuous memory space, regardless of the actual RAM available.
This is achieved by combining:
Main Memory (RAM)
Secondary Storage (Disk)
and dynamically moving data between them as needed.
2. What is Virtual Memory?
Virtual memory is a memory management technique that gives each process the illusion of having a large, contiguous address space, even when physical memory is limited.
Key Idea
Large Virtual Address Space
↓
Physical RAM + Disk
A process sees a large memory space, while the operating system manages the actual storage locations.
3. Why Virtual Memory is Needed
3.1 Limited Physical Memory
RAM is finite.
Large applications may require more memory than physically available.
3.2 Multiprogramming
Many processes must execute simultaneously.
Virtual memory allows more processes to coexist.
3.3 Efficient Memory Utilization
Not all parts of a program are used at the same time.
Only active portions are kept in RAM.
3.4 Process Isolation
Each process gets its own virtual address space.
Processes cannot directly access each other's memory.
4. Core Idea of Virtual Memory
Virtual memory separates:
| Type | Description |
|---|---|
| Logical (Virtual) Memory | What the process sees |
| Physical Memory | Actual RAM |
Instead of loading an entire program into RAM:
Required portions are loaded
Unused portions remain on disk
Pages are fetched when needed
This is called Demand-Driven Execution.
5. Conceptual Model
Virtual Address Space
↓
Memory Management Unit (MMU)
↓
Physical Memory (RAM)
+
Disk (Backing Store)
6. Virtual Address Space
Every process receives its own virtual address space.
Characteristics:
Appears large
Appears contiguous
Starts from address 0
Independent of other processes
Example:
Process A
0
↓
4 GB Virtual Space
Even if the system has only 8 GB RAM.
7. Physical Memory Reality
In reality:
Only Some Pages
↓
Present in RAM
Remaining Pages
↓
Stored on Disk
The process is unaware of this distinction.
8. How Virtual Memory Works
Step 1: Process Creation
The operating system creates a process.
A virtual address space is assigned.
Only essential pages are loaded.
Step 2: CPU Generates Virtual Address
Example:
Virtual Address = 5000
CPU always works with virtual addresses.
Step 3: Address Translation
MMU translates:
Virtual Address
↓
Physical Address
using page tables.
Step 4: Check Page Presence
The operating system checks whether the required page is in RAM.
Case 1: Page Present
Page Found
↓
Continue Execution
Case 2: Page Not Present
Page Missing
↓
Page Fault
9. Page Fault
A page fault occurs when a process accesses a page that is not currently loaded into physical memory.
Example
Process accesses Page 5
Page 5 not in RAM
↓
Page Fault Generated
The operating system must bring the page from disk.
10. Page Fault Handling
When a page fault occurs:
Step 1
OS receives page fault interrupt.
Step 2
Locate required page on disk.
Step 3
Find a free frame in RAM.
If none exists:
Select a victim page
Replace it
Step 4
Load required page into RAM.
Step 5
Update page table.
Step 6
Resume execution.
Flow
Page Fault
↓
Locate Page on Disk
↓
Load into RAM
↓
Update Page Table
↓
Resume Process
11. Demand Paging
Virtual memory primarily uses Demand Paging.
Definition
Pages are loaded into memory only when they are actually accessed.
Example
Suppose a process contains:
Page 0
Page 1
Page 2
Page 3
Page 4
Initially:
RAM
↓
Page 0
Page 1
When Page 3 is accessed:
Page Fault
↓
Load Page 3
Only required pages occupy memory.
12. Components of Virtual Memory System
12.1 Page Table
Maps:
Virtual Page
↓
Physical Frame
Also stores:
Valid bit
Protection bits
Dirty bit
12.2 Backing Store
Disk space used to hold pages not currently in RAM.
Characteristics:
Large capacity
Slow access
Managed by OS
12.3 MMU (Memory Management Unit)
Hardware responsible for:
Address translation
Protection checking
12.4 TLB (Translation Lookaside Buffer)
A cache that stores recent page table entries.
Purpose:
Faster Address Translation
13. Why Virtual Memory Works
Virtual memory succeeds because of the Locality of Reference Principle.
Programs usually access:
Same data repeatedly
Nearby memory locations
Only a small portion of a process is active at a given time.
14. Types of Locality
14.1 Temporal Locality
Recently used data is likely to be used again.
Example:
for(int i=0;i<100;i++)
{
sum += i;
}
Variable sum is repeatedly accessed.
14.2 Spatial Locality
Nearby memory locations are likely to be accessed.
Example:
arr[0]
arr[1]
arr[2]
arr[3]
Sequential array access.
15. Benefits of Virtual Memory
15.1 Large Program Support
Programs can exceed RAM size.
Example:
Program Size = 20 GB
RAM = 8 GB
Still Executable
15.2 Better Memory Utilization
Only active pages occupy RAM.
15.3 Increased Multiprogramming
More processes can execute simultaneously.
15.4 Process Isolation
Each process receives its own address space.
15.5 Simplified Programming
Programmers need not worry about actual memory locations.
16. Performance Considerations
Virtual memory introduces overhead.
Normal Memory Access
RAM Access
↓
Fast
Page Fault Access
Disk Access
↓
Very Slow
Therefore performance heavily depends on page fault frequency.
17. Effective Memory Access Time (EAT)
The average memory access time is:
EAT
=
(1 - p) × Memory Access Time
+
p × Page Fault Service Time
where:
p = Page Fault Rate
Key Insight
Even a small page fault rate can significantly increase access time because disk access is much slower than RAM.
18. Thrashing
Definition
Thrashing occurs when the system spends more time handling page faults than executing processes.
Scenario
Page Fault
↓
Load Page
↓
Replace Page
↓
Another Page Fault
↓
Load Again
↓
Repeat
CPU utilization drops dramatically.
Result
More Swapping
Less Execution
19. Virtual Memory vs Physical Memory
| Feature | Virtual Memory | Physical Memory |
|---|---|---|
| Size | Large | Limited |
| Visibility | Process View | Actual Hardware |
| Location | Logical | RAM |
| Flexibility | High | Low |
| Backed By | RAM + Disk | RAM Only |
| Isolation | Yes | No |
20. Advantages
Large Address Space
Supports huge applications.
Efficient Memory Usage
Loads only required pages.
Better Multiprogramming
Supports more concurrent processes.
Process Protection
Each process has isolated memory.
Flexibility
Programs can execute regardless of physical location.
21. Disadvantages
Complex Implementation
Requires:
Page tables
MMU
Page replacement algorithms
Page Fault Overhead
Disk access is slow.
Additional Hardware Support
Needs MMU and TLB.
Thrashing Risk
Excessive page faults degrade performance.
22. Real-World Analogy
Think of a workspace.
Desk
↓
RAM
Storage Room
↓
Disk
You keep only currently needed files on the desk.
Other files remain in storage.
When required:
Fetch File
↓
Use It
↓
Return It
This is exactly how virtual memory operates.
Most Important Point
Virtual memory gives each process the illusion of having a large, continuous memory space while the operating system dynamically manages data between RAM and disk using demand paging and address translation.