Courses / Computer Science I
Introduction to Computers

Cache and the Memory Hierarchy

Computer Science I 231 words Free to read

The Memory Hierarchy

Fast storage is small and expensive; large storage is slow and cheap. Computers bridge this gap using a memory hierarchy of stacked layers.

LevelSpeedSizeCost
RegistersFastestBytesHighest
CacheVery fastKB-MBHigh
RAMFastGBMedium
SSD / DiskSlowTBLow

A cache is a small, fast memory sitting between the CPU and RAM. It holds copies of recently used data, not unique data.

Common pitfall: A cache never stores separate data from main memory. It only keeps fast copies of what lives in RAM to speed up access.

Four bars, grown from one edge, each slower to cross than the last

Locality and Hits

When the CPU needs data, it checks the cache first. A cache hit is fast, while a cache miss forces a slow fetch from main memory.

Caches succeed because programs show locality of reference:

Locality TypeMeaningCPU Action
TemporalUsed recently?Use again soon
SpatialAddress accessed?Fetch nearby block

The hit rate is the fraction of accesses found in the cache. Because misses are so slow, even small drops in hit rate hurt performance.

Practise this lesson

The explanation above is free to read. The graded practice for this lesson lives in the Tryals app.

10practice questions
2interactive scenes

Introduction to Computers