Find a course

Module 4: Memory and StorageLesson 8 of 1113 min

The Memory Hierarchy

Compare registers, cache, RAM and storage by speed, size and cost, see why caches work so well, and learn what happens when RAM runs out.

Step 1 of 11Learn

Fast, big, cheap: pick two

Ideally a computer would have a huge amount of memory that is also lightning fast and cheap. Physics and economics don't allow it. Fast memory is expensive per byte and must sit physically close to the processor, which limits how much fits. Cheap, huge memory is slow.

So computers use a memory hierarchy: a small amount of very fast memory near the CPU, backed by larger, slower and cheaper layers further away.

To feel the differences, stretch time so that one CPU cycle (about a third of a nanosecond) becomes one second:

  • Reading a register: about 1 second.
  • Reading the level 1 cache: a few seconds.
  • Reading main memory (RAM): around 4 to 5 minutes.
  • Reading from a fast SSD: a few days.
  • Reading from a spinning hard disk: around a year.

That gap is why the hierarchy matters so much. A CPU that had to wait for RAM on every instruction would spend almost all its time idle. Most of the clever engineering in memory systems exists to hide these delays.

Step 2 of 11Compare

The layers at a glance

AspectTypical sizeTypical access timeKeeps data without power?
RegistersA few hundred bytes to a few KBUnder 1 nsNo
L1 cacheTens of KB per coreAbout 1 nsNo
L2 / L3 cacheHundreds of KB to tens of MBAbout 3–20 nsNo
RAM (DRAM)8–64 GB in a typical PCAbout 50–100 nsNo
SSDHundreds of GB to several TBTens of microsecondsYes
Hard disk driveSeveral TBSeveral millisecondsYes

Step 3 of 11Practice · Put in order

Order these from fastest to slowest to access.

Drag the items, or use the arrow buttons, to put them in order.

  1. RAM
  2. L3 cache
  3. Hard disk drive
  4. CPU register
  5. L1 cache
  6. SSD

Step 4 of 11Practice · Diagram

How long one access takes

Each step down the hierarchy is slower, often by ten times or more.

Tap the first level that is more than 1,000 times slower than RAM.

Tap the shape that answers the question, then press Check.

How long one access takesScale: Register, L1 cache, L2 cache, L3 cache, RAM, SSD, Hard disk.10⁻¹1010³10⁵10⁷0.3 nsRegister1 nsL1 cache4 nsL2 cache15 nsL3 cache80 nsRAM100,000 nsSSD10,000,000 nsHard disk
Rough typical latencies; exact figures depend on the hardware.
Register

0.3 ns

L1 cache

1 ns

L2 cache

4 ns

L3 cache

15 ns

RAM

80 ns

SSD

100,000 ns

Hard disk

10,000,000 ns

Step 5 of 11Learn

Why caches work: locality

A cache is a small, fast memory that keeps copies of recently used data. When the CPU needs a value, it checks the cache first. If the data is there, it's a cache hit and the access is fast. If not, it's a cache miss: the data is fetched from the slower level, and a copy is kept in the cache for next time.

Caches work because programs show locality:

  • Temporal locality: data used recently is likely to be used again soon. A loop counter is read and updated thousands of times in a row.
  • Spatial locality: data near recently used data is likely to be used soon. A program processing a list reads item 1, then item 2, then item 3, all stored side by side.

To exploit spatial locality, caches don't fetch single bytes; they fetch whole cache lines, commonly 64 bytes. Reading one item pulls in its neighbors for free.

In typical programs, hit rates of 95% or more in the L1 cache are normal. That's how a processor running at gigahertz speeds can avoid constantly waiting for RAM that is a hundred times slower.

Step 6 of 11Try the calculator

Average memory access time

Average time = hit time + miss rate × miss penalty. Change the hit rate and watch the average.

Average access time
5 ns

Step 7 of 11Note

Step 8 of 11Practice · Work it out

A cache has a hit time of 2 ns, a hit rate of 90% and a miss penalty of 100 ns. What is the average memory access time in nanoseconds?

Step 9 of 11Learn

RAM, swapping and slowdowns

Every open app, browser tab and background service uses RAM. When you open more than fits, the operating system doesn't crash. Instead it moves chunks of memory that haven't been used recently out to storage, into a swap file or page file, and brings them back when needed.

This keeps things working, but storage is thousands of times slower than RAM. If the computer constantly shuffles data back and forth, everything crawls, a state called thrashing. You notice it as a laptop that suddenly takes seconds to switch between apps, with the drive light busy.

The practical lessons:

  • More RAM helps only up to the point where your work fits. Going from 8 GB to 16 GB can transform a sluggish machine; going from 32 GB to 64 GB may change nothing for web browsing.
  • RAM is volatile: its contents vanish when power is lost, which is why unsaved work disappears in a crash.
  • A faster processor won't fix a machine that is running out of memory.

You can check memory use in Task Manager on Windows or Activity Monitor on a Mac.

Step 10 of 11Note

Step 11 of 11Practice · What would you do?

What would you do?

Jordan's laptop has a fast processor and 8 GB of RAM. With 40 browser tabs, a video call and a photo editor open, everything slows to a crawl and the storage activity light stays busy. What is the most likely cause and fix?