Memory hierarchy and performance (AMAT)
Why computers have levels of memory, the principle of locality, hit and miss, hit ratio, average memory access time for one and several levels, hierarchical against simultaneous access, and what a hierarchy buys.
Why it exists
Fast memory is small and expensive; large memory is cheap and slow. No single technology gives all three of large, fast and cheap. A computer therefore uses a hierarchy: a few very fast registers, a small fast cache, a larger and slower main memory, and a very large and very slow disk. The hierarchy works because programs do not use memory at random: they reuse what they used recently and use what is near it. With that behaviour most accesses are served by the fast levels, so the average access is nearly as fast as the fastest level while the total capacity and cost are those of the slowest and cheapest. GATE asks for the average access time, the effect of hit ratios, and how two or three levels combine.
Prerequisites
None. This is a starting point.
No earlier page is needed beyond ordinary arithmetic with ratios.
Precise definitions
The levels (smaller, faster and more expensive per bit toward the top):
| Level | Typical role | Relative speed (orders of magnitude, illustrative) |
|---|---|---|
| Registers | operands of the current instructions | about one clock |
| L1 cache | recently used instructions and data | a few clocks |
| L2, L3 cache | larger, shared | ten or more clocks |
| Main memory (DRAM) | all running programs | about a hundred clocks |
| Disk / SSD | files, swapped-out pages | hundreds of thousands to millions of clocks |
Principle of locality. Programs tend to access a small part of their address space at any time.
- Temporal locality: an item used recently is likely to be used again soon (loop variables, instructions of a loop).
- Spatial locality: items near a recently used item are likely to be used soon (sequential instructions, array elements).
Hardware exploits temporal locality by keeping recently used items in the fast level and spatial locality by moving data in blocks (lines) rather than single words.
Hit and miss. A hit is an access satisfied by the level being examined; a miss is one that must go to the next level. Hit ratio = hits / accesses, miss ratio . The hit time is the time to access the level on a hit and the miss penalty is the extra time to fetch the block from the next level.
Average memory access time (AMAT) for one cache in front of memory:
Two levels, two ways of counting. Let and be the access times of level 1 and level 2 and the hit ratio of level 1.
- Hierarchical (sequential) access: level 2 is accessed only after level 1 has missed, so a miss costs : .
- Simultaneous (parallel) access: both levels are started together and level 2's result is used on a miss: .
The first form equals AMAT with hit time and miss penalty . A GATE question states which of the two to use; "simultaneous" and "hierarchical" are the usual words.
Several levels. With local miss rates (of L1 accesses) and (of L2 accesses):
The global miss rate of L2 (misses per CPU access) is .
Inclusion. In an inclusive hierarchy everything in a level is also in the level below it. The bottom level (disk, via virtual memory) holds everything.
What a hierarchy buys. The average access time approaches that of the top level when hit ratios are high, while the cost per bit approaches that of the bottom level.
How it works
The animation is a direct-mapped cache serving 14 reads. The cache counts cycles: 1 for a hit, 11 for a miss (hit time 1 plus miss penalty 10). The running "avg cycles per access" is the measured AMAT. At the end it is , which equals : hit time plus miss rate (7 misses in 14) times the penalty.
Direct-mapped cache: 8 lines of 4 bytes: start
Cache simulated, not typed8 sets x 1 way, direct mapped. Hit time 1, miss penalty 10 cycles.
| set | way 0 |
|---|---|
| 0 | empty |
| 1 | empty |
| 2 | empty |
| 3 | empty |
| 4 | empty |
| 5 | empty |
| 6 | empty |
| 7 | empty |
Step 1
A direct-mapped cache with 8 lines of 4 bytes and 8-bit addresses: 2 offset bits, 3 index bits, 3 tag bits. Each memory block can live in exactly one line, chosen by the index bits.
Worked examples
EX 1Every formula in a program
Easy#include <stdio.h>
/* average memory access time for one cache level and for a hierarchy */
static double amat(double hit_time, double miss_rate, double miss_penalty) { return hit_time + miss_rate * miss_penalty; }
int main(void) {
/* the 14 accesses of the direct-mapped animation: 7 hits, 7 misses, hit time 1, miss penalty 10 */
int hits = 7, accesses = 14;
double m = 1.0 - (double)hits / accesses;
printf("measured: %d cycles in total = %.2f per access; formula 1 + %.2f x 10 = %.2f\n", hits * 1 + (accesses - hits) * 11, (hits * 1.0 + (accesses - hits) * 11.0) / accesses, m, amat(1, m, 10));
/* two levels, hit ratio h1 = 0.9, t1 = 2, t2 = 20 */
double h1 = 0.9, t1 = 2, t2 = 20;
printf("hierarchical access (level 2 only after a miss): %.1f\n", h1 * t1 + (1 - h1) * (t1 + t2));
printf("simultaneous access (both started together): %.1f\n", h1 * t1 + (1 - h1) * t2);
printf("hit time + miss rate x miss penalty: %.1f\n", amat(t1, 1 - h1, t2));
/* three levels: L1 1 ns, 5%% local miss; L2 10 ns, 10%% local miss; memory 100 ns */
double l1 = 1, l2 = 10, mem = 100, m1 = 0.05, m2 = 0.10;
printf("three levels: AMAT = %.2f ns, global miss rate of L2 = %.3f\n", l1 + m1 * (l2 + m2 * mem), m1 * m2);
/* what the hierarchy buys: without the caches every access would cost 100 ns */
printf("speed-up over main memory alone: %.1f\n", mem / (l1 + m1 * (l2 + m2 * mem)));
/* locality in the numbers: a miss rate of 1/16 (sequential access, 16 words per block) */
printf("sequential access, 16 words per block: miss rate %.4f, AMAT %.2f cycles with a 100-cycle penalty\n", 1.0 / 16, amat(1, 1.0 / 16, 100));
return 0;
}measured: 84 cycles in total = 6.00 per access; formula 1 + 0.50 x 10 = 6.00
hierarchical access (level 2 only after a miss): 4.0
simultaneous access (both started together): 3.8
hit time + miss rate x miss penalty: 4.0
three levels: AMAT = 2.00 ns, global miss rate of L2 = 0.005
speed-up over main memory alone: 50.0
sequential access, 16 words per block: miss rate 0.0625, AMAT 7.25 cycles with a 100-cycle penaltyReading it. The first line is the animation: 84 cycles in 14 accesses, 6 per access, matching the formula. In the two-level example with , , : hierarchical access takes ; simultaneous access takes . With three levels the average is 2 ns instead of the 100 ns of main memory alone (a speed-up of 50), and the global miss rate of L2 is 0.5%.
EX 2Locality and hit ratio
EasyA loop adds the 16 words of an array, one block per 16 words. The first access misses and loads the block; the other 15 words are hits (spatial locality). The miss rate is and with a 1-cycle hit and a 100-cycle penalty the average is cycles. If the loop is executed again with the block still in the cache, even the first access hits (temporal locality) and the average drops to 1 cycle.
EX 3Required hit ratio
MediumA cache has ns and main memory ns. What hit ratio gives an average access time of 20 ns with hierarchical access? , so and . With simultaneous access: , so and . The hierarchical form needs the higher ratio (0.9 against 0.889), because each miss costs more.
EX 4Three levels
MediumL1: 1 ns, local miss rate 5%. L2: 10 ns, local miss rate 10%. Main memory: 100 ns. ns. If L2 is removed and L1 misses go straight to memory the average is ns. L2 cuts the average from 6 to 2 ns. The global L2 miss rate is , but its local miss rate is 10%.
EX 5Effective access time with a page fault
GATE levelThe same formula applies one level lower: main memory as a cache for disk. If memory access takes 100 ns, a page fault service takes 8 ms and the page-fault rate is , the effective access time is ns. Even a tiny fault rate nearly doubles the average time because the disk is so slow.
Cheat box
Cheat box
- Locality: temporal (reuse) and spatial (neighbours). Blocks exploit spatial locality.
- AMAT = hit time + miss rate miss penalty.
- Two levels, hierarchical: . Simultaneous: .
- Multilevel with local rates: . Global L2 miss rate .
- Smaller means faster and more expensive per bit. Hierarchy: near-top speed at near-bottom cost.
- Effective access time of paging: .
Traps and GATE patterns
Connections
Builds on
No prerequisites.
Unlocks
Across subjects
No cross-subject links yet.
Within this subject
The cache is the next topic, starting with how blocks are placed, then replacement and writes and multilevel performance. The main memory is built from chips by memory interfacing, and the disk level is in disk structure. The same formulas drive the performance equations.
PYQs
No PYQs attached to this concept yet
Previous year questions are being added section by section from your PYQ book, each with my own re-checked answer. Nothing is shown from memory.
Self-check
Question 1
A cache has a hit time of 2 ns, a miss rate of 4% and a miss penalty of 50 ns. What is the AMAT?
Show answerHide
ns.
Question 2
ns, ns, . Average access time with simultaneous access and with hierarchical access?
Show answerHide
Simultaneous: ns. Hierarchical: ns.
Question 3
Which kind of locality makes caches use blocks of several words?
Show answerHide
Spatial locality: after a word is used its neighbours are likely to be used, so the whole block is fetched.
Question 4
L1 misses 10% of CPU accesses and L2 misses 25% of the accesses it receives. What is the global miss rate of L2?
Show answerHide
.
Revision card
One-screen revision
- Hierarchy: small and fast on top, large and cheap below; works because of temporal and spatial locality.
- AMAT = hit time + miss rate penalty. Hierarchical: ; simultaneous: .
- Multilevel: use local miss rates; global .
References
- ·Hennessy and Patterson, Computer Organization and Design, Chapter 5 Large and Fast: Exploiting Memory Hierarchy (introduction, the basics of caches)
- ·Hamacher, Vranesic, Zaky, Computer Organization, 5th ed., Chapter 8 The Memory System (memory hierarchy, cache memories, performance considerations)
- ·Silberschatz, Galvin, Gagne, Operating System Concepts, Chapter 9/10 Main Memory and Virtual Memory (effective access time)
Verification
The average-access-time formulas and every number on the page (one level, hierarchical and simultaneous two-level access, three levels with local miss rates, the 50 times speed-up, the sequential-access miss rate) are recomputed in scripts/enum-checks/coa_mem.py and by the C program (npm run verify:c). The measured average of the animation (84 cycles for 14 accesses) is the exact simulation of the cache trace re-checked in tests/coa-mem-traces.test.ts. The relative speeds and sizes of the levels are orders of magnitude for illustration, not data for a particular machine. Textbook chapter titles are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand (long C programs were not re-read line by line: they are compiled and compared by npm run verify:c, and their printed outputs were checked against the prose). No defect found. Status raised to self-checked; verified needs a textbook or human check.
last verified: never
My notes (saved in this browser)