How Virtual Memory Lets Your Disk Pretend to Be RAM
The article explains how operating systems use virtual memory to extend physical RAM with disk space, covering concepts like paging, page tables, multi‑level tables, swap space, replacement algorithms (LRU, FIFO, LFU), and practical Python and JVM examples.
A programmer with an 8 GB RAM, 512 GB SSD opens dozens of Chrome tabs, IDEs, and Docker containers; when physical memory fills, the OS keeps the programs running by using virtual memory.
Core Concept
What is Virtual Memory?
Virtual Memory = simulated "large memory" using the disk.
物理内存(8GB):
┌────────────┐
│ 应用程序1 │
├────────────┤
│ 应用程序2 │
├────────────┤
│ 应用程序3 │
├────────────┤
│ ...满了... │
└────────────┘
虚拟内存(512GB):
┌────────────┐
│ 应用程序1 │
├────────────┤
│ 应用程序2 │
├────────────┤
│ 应用程序3 │
├────────────┤
│ 操作系统 │
├────────────┤
│ 更多应用 │
├────────────┤
│ ...更多 │
└────────────┘Key Ideas
Each process has its own complete address space.
A 32‑bit process believes it has 4 GB of memory.
Physical RAM may be only 8 GB.
Insufficient memory is supplemented by disk "swap" space.
Functions of Virtual Memory
Address isolation : each program has an independent address space.
Capacity extension : programs can use more memory than physically present.
Simplified programming : developers need not manage physical memory directly.
Memory protection : prevents out‑of‑bounds accesses.
Technical Details
Paging Mechanism
Paging = split memory into fixed‑size "pages".
物理内存分页(4KB):
┌────┬────┬────┬────┬────┬────┬────┬────┐
│ P0 │ P1 │ P2 │ P3 │ P4 │ P5 │ P6 │ P7 │
└────┴────┴────┴────┴────┴────┴────┴────┘
虚拟内存分页(4KB):
┌────┬────┬────┬────┬────┬────┬────┬────┐
│ V0 │ V1 │ V2 │ V3 │ V4 │ V5 │ V6 │ V7 │
└────┴────┴────┴────┴────┴────┴────┴────┘
页表(映射关系):
V0 → P2
V1 → P5
V2 → P0
V3 → P7
…Page Table Structure
虚拟地址: [ 页号 (VPN) ] [ 页内偏移 (Offset) ]
20位 12位 (4KB页)
物理地址: [ 物理页号 (PPN) ] [ 页内偏移 (Offset) ]
20位 12位 (4KB页)Page Table Entry
┌───┬───┬───┬────────┬────────┐
│ V │ R │ M │ PPN │ 其他 │
├───┴───┴───┴────────┴────────┤
V = Valid bit
R = Reference (recently accessed)
M = Modified/Dirty
PPN = Physical Page NumberMulti‑Level Page Tables
Single‑level tables on a 32‑bit system would need ~1 M entries (4 B each), consuming 4 MB. Multi‑level tables use hierarchical indexing to reduce storage.
一级页表(4KB):
┌────┬────┬────┬────┐
│ │ P2 │ │ P3 │
└─┬──┴─┬──┴────┴────┘
│
▼
二级页表(P2指向):
┌────┬────┬────┬────┐
│ P8 │ P9 │P10 │P11 │
└────┴────┴────┴────┘Page Replacement Algorithms
When physical memory is full, some pages are swapped out to disk.
1. LRU (Least Recently Used)
class LRUReplacer:
def __init__(self, capacity):
self.capacity = capacity
self.pages = [] # ordered by access time
def replace(self):
"""Evict the least recently used page"""
return self.pages.pop(0) # oldest
def access(self, page):
"""Access a page"""
if page in self.pages:
self.pages.remove(page)
self.pages.append(page)2. FIFO (First‑In‑First‑Out)
class FIFOReplacer:
def __init__(self, capacity):
self.capacity = capacity
self.pages = [] # ordered by insertion time
def replace(self):
"""Evict the earliest inserted page"""
return self.pages.pop(0)3. LFU (Least Frequently Used)
class LFUReplacer:
def __init__(self, capacity):
self.capacity = capacity
self.pages = {} # {page: access_count}
def replace(self):
"""Evict the page with the fewest accesses"""
return min(self.pages, key=self.pages.get)Swap Space
# View swap space
swapon -s
# Example output:
# Filename Type Size Used Priority
# /dev/sda5 partition 8GB 2GB -2
# Create a swap file
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfilePractical Applications
Scenario 1: Memory‑leak detection (Python)
import tracemalloc
tracemalloc.start()
data = []
for i in range(1000000):
data.append({"id": i, "value": i * 2})
current, peak = tracemalloc.get_traced_memory()
print(f"Current: {current/1024/1024:.1f} MB")
print(f"Peak: {peak/1024/1024:.1f} MB")
tracemalloc.stop()Scenario 2: JVM memory layout
// JVM memory options
// -Xms256m (initial heap size)
// -Xmx1024m (maximum heap size)
// -Xss1m (thread stack size)
// Memory generations
// Young Generation: Eden + Survivor
// Old Generation
// Metaspace (method area)
// GC trigger conditions
// - Young GC: Eden full
// - Full GC: Old generation fullScenario 3: Large file memory‑mapping (Python)
import mmap
with open("large_file.dat", "r+b") as f:
mm = mmap.mmap(f.fileno(), 0) # map file to virtual memory
print(mm[0:100]) # read first 100 bytes
mm.close()Key Takeaways
Virtual address = page number + offset.
Physical address = physical page number + offset.
Page tables map virtual pages to physical pages.
Page‑replacement policies include LRU, FIFO, and LFU.
Virtual memory gives programs a larger address space, isolates processes, and simplifies memory management, but excessive paging (thrashing) can severely degrade performance because disk I/O is 100–1000× slower than RAM.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
IT Learning Made Simple
Learn IT: using simple language and everyday examples to study.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
