Cache & Virtual Memory
Cache is a tiny, ultra-fast memory between the CPU and RAM that speeds up access to frequently used data. Virtual memory uses the hard drive to extend RAM when physical memory runs out.
Learning Objectives
- 11.3.4.3 Explain the purpose of cache memory
- 11.3.4.2 Explain the purpose of virtual memory
Conceptual Anchor
The Workspace Analogy
Cache = the few papers right in front of you on your desk (instant access). RAM = your desk drawer (fast, but need to open it). Virtual memory = a filing cabinet across the room (slow, but extends your capacity when the desk is full). You always work fastest with what's right in front of you.
Rules & Theory
Memory Hierarchy
Speed ↑ Cost ↑ Size ↓
┌──────────────────────┐
│ CPU Registers │ ← Fastest, smallest
├──────────────────────┤
│ Cache (L1/L2/L3) │ ← Very fast, small (KB–MB)
├──────────────────────┤
│ RAM │ ← Fast, medium (GB)
├──────────────────────┤
│ Virtual Memory │ ← Slow (uses HDD/SSD)
├──────────────────────┤
│ Secondary Storage │ ← Slowest, largest (TB)
└──────────────────────┘Cache Memory
| Level | Location | Speed | Size |
|---|---|---|---|
| L1 | Inside CPU core | Fastest | 32–128 KB per core |
| L2 | Inside/near CPU core | Fast | 256 KB – 1 MB per core |
| L3 | Shared across all cores | Slower | 4–64 MB total |
How Cache Works
CPU needs data →
Check L1 cache → HIT? Use it (fast!) ✓
Miss → Check L2 cache → HIT? Use it ✓
Miss → Check L3 cache → HIT? Use it ✓
Miss → Fetch from RAM (slower)
Copy to cache for next timeVirtual Memory
| Feature | Description |
|---|---|
| What it is | A section of the hard drive used as an extension of RAM |
| When used | When physical RAM is full |
| How it works | OS swaps inactive pages from RAM to disk (paging) |
| Page file | The file on disk that stores swapped data (pagefile.sys on Windows) |
| Advantage | Allows more programs to run than physical RAM allows |
| Disadvantage | Much slower than RAM; excessive use causes thrashing |
Thrashing
When the OS spends more time swapping pages between RAM and disk than actually running programs, performance drops dramatically. This is called thrashing. Solution: add more physical RAM.
Common Pitfalls
Cache = RAM
Cache is NOT RAM. Cache is smaller, faster, and more expensive (SRAM). RAM is larger and slower (DRAM). Cache sits between the CPU and RAM in the memory hierarchy.
Virtual Memory = More RAM
Virtual memory is NOT additional RAM. It's hard drive space pretending to be RAM. It's hundreds of times slower than real RAM — it's a workaround, not a replacement.
Tasks
Name the 3 levels of cache and their typical sizes.
Explain why cache improves CPU performance even though it's much smaller than RAM.
A user's computer becomes very slow when many programs are open. Explain what is happening in terms of virtual memory and suggest a solution.
Self-Check Quiz
Q1: What is the purpose of cache memory?
Q2: What is virtual memory?
Q3: What is thrashing?