Edexcel International Advanced Level Computer Science, point 1.1.6
Definition
The memory hierarchy is the arrangement of a computer's storage in levels, from the fastest, smallest and most expensive (next to the CPU) to the slowest, largest and cheapest (furthest away).
Why it exists
No single type of memory is fast, large and cheap at the same time. So computers combine several, and keep the data most likely to be needed in the fastest levels.
The levels
a. CPU registers
- Function: hold the values the CPU is working on right now.
- Characteristics: the fastest memory, inside the CPU, with very small capacity (a few hundred bytes).
b. Cache (levels 1, 2 and 3)
- Function: hold copies of recently or frequently used instructions and data from RAM.
- Level 1 (L1): the fastest and smallest, built into each core (tens of KB). Often split into instruction and data caches.
- Level 2 (L2): larger and slower than L1 (hundreds of KB to a few MB). Depending on the CPU design, it is either per core or shared between several cores.
- Level 3 (L3): larger and slower again (several MB to tens of MB), usually shared by all cores.
- Operation: when the CPU needs something, it checks L1, then L2, then L3. A cache hit means it's found, and a cache miss means the next level must be checked.
c. Main memory (RAM)
- Function: holds programs and data currently in use.
- Characteristics: volatile, much larger (GBs), slower than cache.
d. Secondary storage
- Function: long-term storage of programs and data (SSD, hard disk, optical or flash).
- Characteristics: non-volatile, very large (hundreds of GB to TB), much slower than RAM, and cheapest per byte.
Comparing the levels
The figures are typical orders of magnitude, not exact values.
| Level | Typical size | Typical access time | Cost per byte | Volatile? |
|---|---|---|---|---|
| Registers | Hundreds of bytes | Under 1 ns | Highest | Yes |
| L1 cache | Tens of KB | About 1 ns | Very high | Yes |
| L2 cache | Hundreds of KB to a few MB | A few ns | High | Yes |
| L3 cache | Several MB to tens of MB | 10 to 20 ns | High | Yes |
| RAM | Several GB | About 100 ns | Medium | Yes |
| SSD | Hundreds of GB | About 100 µs | Low | No |
| Hard disk | Hundreds of GB to TB | Several ms | Lowest | No |
Why it works: locality
Programs tend to reuse the same data and nearby data:
- Temporal locality: something used just now is likely to be used again soon (for example a loop counter).
- Spatial locality: items near the one just used are likely to be needed next (for example the next element of an array). So blocks of nearby data are copied into cache together.
Because of this, most requests are answered by the fast levels, and the memory seems almost as fast as the fastest.
Benefits
- Speed close to the fastest level, at a cost close to the cheapest.
- The CPU spends less time waiting.
- Large amounts of permanent storage are available cheaply.
Drawbacks
- Complexity: hardware is needed to move data between levels and keep copies consistent.
- Cache misses cost extra time because each level is checked first.
- Fast levels are small, so useful data can be pushed out.
- Higher cost and power use if the fast levels are made bigger.
Worked example
Task: a cache has an access time of 2 ns and RAM has an access time of 100 ns. 95% of requests are found in the cache. Estimate the average access time.
- A hit (95%) takes 2 ns.
- A miss (5%) takes the cache check plus a RAM access: ns.
That is over 14 times faster than going to RAM every time (100 ns), even though the cache is tiny. If the hit rate fell to 80%, the average would rise to ns, which shows why a high hit rate matters.
Exam tip: put the levels in order, and say how speed, capacity and cost change as you move down. Explain the purpose: to give the CPU fast access to the data it needs most while keeping large storage affordable. You may be asked to complete a diagram of the hierarchy.
Check yourself
List the levels of the memory hierarchy from fastest to slowest
CPU registers, L1 cache, L2 cache, L3 cache, main memory (RAM), secondary storage.
What happens to speed, size and cost per byte as you move away from the CPU?
Speed falls, size increases and cost per byte falls.
What is a cache miss?
When the data the CPU needs isn't in the cache, so the next level of memory must be searched, which takes longer.
Why does a small cache improve performance so much?
Programs reuse the same data and nearby data (locality), so most requests are found in the cache and are answered quickly.
Why is the memory hierarchy needed?
No single type of memory is fast, large and cheap, so a combination is used with the most-needed data kept in the fastest levels.