Virtual Memory

Memory Organization & Management

What is Virtual Memory?

Virtual memory is a crucial memory management technique that provides an "idealized abstraction" of storage resources, separating logical memory from physical memory.

📚 Key Concept

Virtual memory allows programs to use more memory than physically available by utilizing specialized hardware to manage memory resources efficiently through a memory hierarchy.

Memory Hierarchy

⚡

Cache Memory

Fastest, smallest

💾

Main Memory

Physical RAM

🗄️

Auxiliary Memory

Disk storage

Why Virtual Memory?

🔄 Program Isolation

Each program operates in its own virtual address space, preventing interference

📈 Memory Expansion

Run programs larger than physical memory by swapping pages

🛡️ Protection

Hardware-enforced memory protection between processes

🎯 Simplified Programming

Programmers work with consistent virtual addresses

Address Space and Memory Space

Understanding the Distinction

Address Space: The range of virtual addresses used by the CPU and visible to programs

Memory Space: The range of physical addresses available in main memory (RAM)

Visual Representation

Virtual Address Space

0x0000 - Program Code
0x1000 - Program Data
0x2000 - Heap
0x3000 - Stack
0xFFFF - System
→

Physical Memory Space

Frame 0
Frame 1
Frame 2
Frame 3
Frame 4

Address Translation Example

Virtual address space can be larger than physical memory!

Address Mapping Using Pages

What are Pages?

The virtual address space is divided into fixed-size blocks called pages. Physical memory is divided into equally-sized blocks called frames.

Page Structure

4KB

Typical Page Size

12

Offset Bits (4KB)

20

Page Number Bits (32-bit)

Page Decomposition

Virtual Address (32-bit): [Page Number: 20 bits] | [Page Offset: 12 bits]
↓        ↓
Page Table Direct Copy
Lookup to Physical
↓ ↓
[Frame Number: 20 bits] | [Frame Offset: 12 bits]
Physical Address (32-bit)

Associative Memory Page Table

Translation Lookaside Buffer (TLB)

An associative memory cache that stores recent page-to-frame translations for fast address lookup.

Page Table Structure

Page 0
Frame: 5
Valid: ✓
Page 1
Frame: 2
Valid: ✓
Page 2
Frame: 7
Valid: ✓
Page 3
Frame: -
Valid: ✗
Page 4
Frame: 1
Valid: ✓
Page 5
Frame: 9
Valid: ✓

Address Translation Process

Step 1: CPU generates virtual address
Step 2: Extract page number from virtual address
Step 3: Search TLB for page number (parallel search)
Step 4: If TLB hit → use frame number; If TLB miss → access page table
Step 5: Combine frame number with page offset
Step 6: Access physical memory at computed address

Page Replacement Algorithms

Page Fault

When a program references a page not currently in physical memory, a page fault occurs. The operating system must load the required page from auxiliary storage.

Common Replacement Algorithms

🔄 FIFO (First-In-First-Out)

Replace the oldest page in memory. Simple but may remove frequently-used pages.

🎯 LRU (Least Recently Used)

Replace the page that hasn't been used for the longest time. Better performance but requires tracking.

🎲 Optimal

Replace the page that won't be used for the longest time in the future. Theoretical best, impractical in reality.

⚡ Clock (Second Chance)

Circular list with reference bits. Approximates LRU with less overhead.

Interactive Virtual Memory Simulator

Virtual Memory Access Simulator

Enter a virtual address to see the translation process

0

TLB Hits

0

TLB Misses

0

Page Faults

0%

Hit Rate

Start Learning Again