Virtual Memory
Virtual memory is a memory-management technique that allows a process to use a large logical/virtual address space even when the available physical RAM is smaller.
Virtual memory makes the system appear to have more memory than the physically installed RAM by using secondary storage as an extension of memory.
For example:
Physical RAM = 8 GB Virtual address space = 16 GB
A program can have a 16 GB virtual address space even though only 8 GB of physical RAM exists. Not all of that virtual address space needs to be in RAM at the same time.
1. Why Do We Need Virtual Memory?
Suppose your computer has:
RAM = 4 GB
But you want to run:
Chrome → 2 GB VS Code → 1.5 GB VM → 3 GB Other apps → 1 GB
Total demand:
2 + 1.5 + 3 + 1 = 7.5 GB
But RAM is only:
4 GB
Without virtual memory, the system would have much less flexibility.
With virtual memory:
Virtual Memory | +--------+--------+ | | ↓ ↓ RAM SSD/HDD Active pages Other pages
The OS keeps actively needed portions in RAM and can keep less-active portions elsewhere.
2. Basic Idea
A process sees:
+-------------------------+ | Virtual Address | | Space | +-------------------------+
The actual physical memory is:
+-------------------------+ | Physical RAM | +-------------------------+
The OS and hardware translate between them.
Process ↓ Virtual Address ↓ MMU / Page Tables ↓ Physical Address ↓ RAM
If the required page isn't currently available in RAM:
Page Fault ↓ OS loads page from storage ↓ RAM
3. Virtual Memory Is Not the Same as RAM
This is important.
RAM
Actual physical memory:
RAM → Physical hardware
Virtual memory
An abstraction provided by the OS and hardware:
Virtual Memory ↓ Virtual address space ↓ Mapped to physical memory
Virtual memory does not mean that the computer physically has additional RAM.
4. Virtual Address Space
Each process can have its own virtual address space.
For example:
Process A Virtual Address Space +----------------+ | Code | | Data | | Heap | | Stack | +----------------+
Process B:
Process B Virtual Address Space +----------------+ | Code | | Data | | Heap | | Stack | +----------------+
They can use similar virtual addresses while being mapped to different physical memory.
This provides process isolation.
5. Virtual Memory and Paging
Virtual memory is closely connected to paging.
Remember:
Virtual Memory → Pages Physical RAM → Frames
Example:
Virtual Memory +------+------+ | P0 | P1 | +------+------+ | P2 | P3 | +------+------+ | P4 | P5 | +------+------+
Physical RAM:
+------+------+ | F0 | F1 | +------+------+ | F2 | F3 | +------+------+
Some virtual pages may currently be in RAM:
P0 → F2 P2 → F0 P5 → F3
Other pages may not currently be in RAM.
6. Demand Paging
One of the most important virtual-memory techniques is demand paging.
Demand paging loads a page into physical memory only when that page is actually needed.
Instead of loading the entire program into RAM:
Program ↓ Load everything ↓ RAM
the system can do:
Program ↓ Load required pages ↓ RAM
For example:
Program has: P0 P1 P2 P3 P4 P5 P6 P7
Initially:
RAM: P0 P2 P5
When the program needs P6:
P6 not in RAM ↓ Page Fault ↓ Load P6 ↓ RAM
7. Page Fault
A page fault occurs when a process references a virtual page that is not currently present in physical memory.
Example:
CPU ↓ Access Page 7 ↓ Page Table ↓ Page present? | No ↓ Page Fault
The OS then handles the fault.
8. Page Fault Handling
A simplified page-fault process:
CPU references page | ↓ Check page table | Page present? / \ Yes No | | ↓ ↓ Continue Page Fault | ↓ Check whether access is valid | +------+------+ | | Valid Invalid | | ↓ ↓ Find free frame Terminate/ | exception ↓ Read page from storage | ↓ Place page into frame | ↓ Update page table | ↓ Retry instruction
9. Page Replacement
What if there is no free frame when a required page needs to be loaded?
Then the OS has to remove another page from RAM.
This is called:
Page replacement
Example:
RAM is full: F1 → P1 F2 → P3 F3 → P5 F4 → P7
Process needs P9.
There is no free frame.
So the OS chooses a page to remove:
P5 → removed P9 → loaded
This decision is made using a page replacement algorithm.
10. Page Replacement Algorithms
Important algorithms include:
Page Replacement | +── FIFO | +── Optimal | +── LRU | +── Second Chance / Clock | +── NRU and other approaches
The most important introductory algorithms are:
- FIFO
- Optimal
- LRU
11. FIFO Page Replacement
FIFO = First In, First Out
The page that entered memory first is removed first.
Example:
Pages in RAM: P1 → P2 → P3 ↑ Oldest
If P4 arrives:
P1 removed P2 → P3 → P4
Advantage
- Simple
- Easy to implement
Disadvantage
It may remove a frequently used page simply because it is old.
12. Optimal Page Replacement
The Optimal algorithm replaces the page that will not be used for the longest time in the future.
Example:
Current pages: P1 P2 P3
Suppose future references are:
P2 → P1 → P4 → P2 → P3
If P4 is needed and one page must be removed:
- P1 is needed soon
- P2 is needed soon
- P3 is needed later
So:
P3 → replaced P4 → loaded
Important
Optimal replacement gives the theoretical minimum number of page faults for a reference string, but:
The OS cannot generally implement true Optimal replacement in real time because it does not know the future.
It is mainly used as a benchmark.
13. LRU
LRU = Least Recently Used
It replaces the page that has not been used for the longest time in the past.
Example:
Pages: P1 P2 P3
Recent usage:
P1 → P3 → P1 → P2
If a replacement is needed, the least recently used page is selected based on the recent history.
Advantage
Often performs well because programs commonly exhibit locality of reference.
Disadvantage
Exact LRU implementation can require additional hardware/software support and can be expensive.
14. FIFO vs LRU vs Optimal
| Algorithm | Replacement choice | Practical? |
|---|---|---|
| FIFO | Oldest page | Yes |
| LRU | Least recently used | Yes/approximated |
| Optimal | Page used farthest in future | No, theoretical |
Memory trick:
FIFO = oldest
LRU = least recently used
Optimal = used farthest in future
15. Belady's Anomaly
A famous property of FIFO is Belady's anomaly.
Normally, we expect:
More frames → fewer page faults
But with some reference strings under FIFO:
More frames → MORE page faults
This surprising behavior is called:
Belady's anomaly
It is primarily associated with FIFO and certain non-stack page-replacement algorithms.
16. Locality of Reference
Programs usually don't access memory randomly.
They tend to repeatedly access nearby or recently used data.
This is called:
Locality of reference
Two important types:
Temporal locality
Recently used data is likely to be used again soon.
Example:
Loop variable Function instructions Frequently used variable
Spatial locality
If one memory location is accessed, nearby locations are likely to be accessed soon.
Example:
Array: A[0] A[1] A[2] A[3]
Programs often access these sequentially.
Locality is one reason caching and virtual-memory techniques work effectively.
17. Working Set
The working set is approximately the collection of pages a process is actively using during a particular period.
Example:
Process has pages: P0 P1 P2 P3 P4 P5 P6 P7
Currently active:
P1 P2 P4 P5
These pages form the process's current working set.
If enough of the working set is kept in RAM, the process can execute efficiently.
18. Thrashing
Thrashing occurs when the system spends excessive time handling page faults and moving pages between RAM and storage instead of performing useful computation.
Example:
CPU needs P1 ↓ Page fault ↓ Load P1 ↓ CPU needs P2 ↓ Page fault ↓ Load P2 ↓ CPU needs P3 ↓ Page fault ↓ ...
The system is constantly paging.
Useful execution ↓↓↓ Paging activity ↑↑↑
This causes severe performance degradation.
19. Why Thrashing Happens
Common reasons include:
- Too many processes competing for memory
- Too few available frames
- Working sets do not fit in RAM
- High page-fault rate
- Poor memory allocation
Conceptually:
Too many processes ↓ Not enough frames per process ↓ More page faults ↓ More paging ↓ Less useful CPU work ↓ THRASHING
20. How to Control Thrashing
Possible approaches include:
1. Reduce degree of multiprogramming
Run fewer processes simultaneously.
2. Give processes more frames
Allow their working sets to stay in memory.
3. Working-set model
Try to keep the active working set in RAM.
4. Page-fault frequency control
Monitor page-fault rates and adjust frame allocation.
21. Demand Paging vs Pre-Paging
Demand Paging
Load a page only when needed.
Need page ↓ Page fault ↓ Load page
Pre-Paging
Load pages in advance based on an expected future need.
Predict required pages ↓ Load them together ↓ Reduce future page faults
Pre-paging can be useful when predictions are accurate, but loading unnecessary pages wastes I/O and memory.
22. Swap Space
Operating systems can reserve storage space for memory-management purposes.
It is often called:
Swap space
or, depending on the OS:
Swap partition Swap file Pagefile
Conceptually:
RAM ↓ Page moved out ↓ Swap storage
When needed again:
Swap storage ↓ Page brought back ↓ RAM
Important:
Storage is much slower than RAM, so heavy swapping/paging can significantly hurt performance.
23. Virtual Memory and Security
Virtual memory also helps with isolation.
Suppose:
Process A Virtual address 1000 ↓ Physical frame 20
Process B can also have:
Virtual address 1000 ↓ Physical frame 50
The same virtual address does not necessarily refer to the same physical memory.
This helps keep processes separated.
24. Copy-on-Write
Virtual memory also enables copy-on-write (COW).
Suppose a process creates another process.
Instead of immediately copying all memory:
Process A | +---- Shared pages ----+ | Process B
Both can initially share physical pages.
If one process writes to a shared page:
Process A writes ↓ Page copied ↓ A → new copy B → original
This saves memory and reduces unnecessary copying.
25. Memory-Mapped Files
Virtual memory can also be used to map files into a process's virtual address space.
Conceptually:
File on storage ↓ Memory mapping ↓ Virtual address space ↓ Process accesses file like memory
This can be useful for:
- Large files
- Shared data
- Databases
- Shared libraries
- Inter-process communication
26. Virtual Memory and TLB
Remember the TLB from paging.
The complete path can be:
CPU ↓ Virtual Address ↓ TLB ↓ Page Table ↓ Physical Frame ↓ RAM
If the page isn't present:
Page Table ↓ Page Fault ↓ Storage ↓ RAM
So TLB improves address translation, while page replacement handles which pages should be in RAM.
27. Effective Access Time
Virtual memory systems may involve several possible delays:
TLB lookup Page-table access Memory access Page-fault handling Disk/SSD I/O
A page fault is extremely expensive compared with ordinary RAM access.
Simplified idea:
Normal access ≈ nanoseconds Page fault + storage access ≈ much slower
Therefore, reducing page faults is extremely important for performance.
28. Page Fault Rate
A useful concept is:
Page Fault Rate = Number of Page Faults --------------------- Total Memory References
For example:
Memory references = 10,000 Page faults = 100
Then:
100 / 10,000 = 0.01
or:
1%
A high page-fault rate can indicate memory pressure.
29. Virtual Memory Example
Suppose:
RAM = 4 GB Virtual address space = 8 GB
A process has pages:
P0 P1 P2 P3 P4 P5 P6 P7
Only some pages are currently in RAM:
RAM: P0 P2 P4 P7
Process requests:
P5
But:
P5 ∉ RAM
Therefore:
Page Fault ↓ Find free frame ↓ Load P5 ↓ Update page table ↓ Execute instruction
If RAM is full:
P0 P2 P4 P7
The OS must choose one page to replace.
For example, using LRU:
P2 = least recently used
Then:
P2 → removed P5 → loaded
30. Complete Virtual Memory Architecture
CPU | ↓ Virtual Address | ↓ TLB / \ Hit Miss | | ↓ ↓ Frame Number Page Table | Page Present? / \ Yes No | | ↓ ↓ Frame Page Fault | | | ↓ | Page Replacement | | | ↓ | Storage / Swap | | | ↓ | Load Page | | +-------------+ | ↓ Physical Address | ↓ RAM
31. Advantages of Virtual Memory
1. Larger address spaces
Programs can have large virtual address spaces.
2. Better multiprogramming
More processes can coexist than would fit entirely in RAM.
3. Process isolation
Each process gets its own virtual address space.
4. Efficient RAM usage
Only actively needed pages need to occupy RAM.
5. Supports demand paging
Programs can load pages as needed.
6. Sharing
Pages can be shared where appropriate.
7. Copy-on-write
Avoids unnecessary memory copying.
32. Disadvantages of Virtual Memory
1. Page-fault overhead
Page faults can be expensive.
2. Storage is much slower than RAM
Heavy paging reduces performance.
3. Page-table overhead
Page tables consume memory.
4. TLB/memory-management complexity
Address translation requires hardware and OS support.
5. Thrashing
Excessive paging can severely degrade performance.
6. More complex OS design
Virtual memory requires sophisticated memory management.
33. Important Differences
Virtual Memory vs Physical Memory
| Virtual Memory | Physical Memory |
|---|---|
| Logical abstraction | Actual RAM |
| Seen by processes | Actual hardware |
| Larger address space possible | Limited by installed RAM |
| Managed through page tables etc. | Actual memory chips |
| Can map to RAM/storage | Exists physically in RAM |
Paging vs Virtual Memory
These are not exactly the same thing.
Paging is a memory-management mechanism:
Pages ↔ Frames
Virtual memory is a broader concept that allows processes to use virtual address spaces larger than physical memory and can use mechanisms such as paging.
Virtual Memory | +── Paging +── Demand Paging +── Page Replacement +── Swap/Backing Storage +── Protection
34. Most Important Page Replacement Algorithms
| Algorithm | Idea |
|---|---|
| FIFO | Remove oldest page |
| Optimal | Remove page needed farthest in future |
| LRU | Remove least recently used page |
| Second Chance | FIFO with reference-bit consideration |
| Clock | Efficient implementation related to Second Chance |
For exams, FIFO, Optimal and LRU are especially important.
35. Key Numerical Concepts
For page-replacement problems, you may be given:
Reference String: 7 0 1 2 0 3 0 4 ...
and:
Number of frames = 3
Then you calculate:
Page faults Page hits Page replacements
For example:
Reference → 7 Frames → [7, -, -] Fault → Yes Reference → 0 Frames → [7, 0, -] Fault → Yes Reference → 1 Frames → [7, 0, 1] Fault → Yes
Later references may produce either hits or faults depending on the replacement algorithm.
🧠 Exam Revision
Definition
Virtual memory is a memory-management technique that provides processes with a large virtual address space by mapping virtual addresses to physical memory and, when necessary, using secondary storage to hold pages that are not currently in RAM.
Core concepts:
Virtual Memory │ ├── Virtual Address Space ├── Paging ├── Demand Paging ├── Page Fault ├── Page Replacement │ ├── FIFO │ ├── Optimal │ ├── LRU │ └── Clock / Second Chance ├── TLB ├── Swap Space ├── Locality of Reference ├── Working Set ├── Thrashing ├── Copy-on-Write └── Memory-Mapped Files
Easy memory trick:
“Virtual memory gives the illusion of more memory; paging manages it; page faults bring needed pages; page replacement makes room; too much paging causes thrashing.”
Most important flow:
Program ↓ Virtual Address ↓ Page Table / TLB ↓ Is page in RAM? ↓ ┌───────────────┐ │ Yes │ No ↓ ↓ RAM Page Fault ↓ Load / Replace Page ↓ RAM