Systems study
Offline — showing the cached copy.
Home / OS Theory / 2. The cache hierarchy

2. The cache hierarchy

One-liner. Small, fast, expensive memory close to the core; each level trades capacity for latency.

Memorize

LevelTypical sizeLatency (cycles)
L132–64 KB~4–5
L2256 KB–1 MB~12–20
L38–64 MB (shared)~40–70
DRAMGBs~100–300
  • A cache line is 64 bytes; you always fetch a whole line, even for one byte.
  • Address splits into tag / set index / block offset; associativity decides how many lines share a set.
  • Write-back + write-allocate is the default: writes hit the cache and flush later.
  • Inclusive caches duplicate lines upward; exclusive/victim caches avoid duplication.
  • Hardware prefetchers detect streams and strides; unpredictable access defeats them.
  • Cache is why sequential memory access beats pointer chasing by an order of magnitude.
MEMORY HIERARCHY REGISTERS bytes ~0 cycles L1/L2 KB per core L3 MB shared DRAM GB ~100+ cycles SSD/DISK TB µs–ms
Memory hierarchy

Trap. Optimizing instruction count while ignoring memory access patterns — a cache miss can cost more than hundreds of arithmetic ops. Do this. Measure with perf stat (cache-misses, LLC-load-misses) before micro-tuning.

Check

Why can reading one byte be as expensive as reading 64?