Sub topic 1
Why memory is a hierarchy
Computers store data in several kinds of memory instead of one. This chapter explains why, what each level looks like, and what rules the levels follow. The later chapters build cache mapping and timing formulas on this foundation.
Prerequisites
This chapter assumes you already know the following:
- Binary and hexadecimal numbers, and powers of two (1 KiB = 2^10 bytes).
- Logarithms base 2, for example log2(64) = 6.
- Byte-addressable memory: each address names one byte, and a word is a fixed group of bytes the CPU handles at once.
- The basic cycle in which the CPU fetches instructions and data from memory.
- Weighted averages. They matter more in later chapters, but you will use one here.
The problem: speed, size and cost pull apart
An ideal memory would be as fast as the CPU, as large as all the data you want to keep, and cheap. No technology offers all three at once.
- Fast memory is expensive per bit. SRAM (static RAM) stores each bit in about six transistors. It is fast, but it takes a lot of chip area and power.
- Dense memory is slower. DRAM (dynamic RAM) stores a bit as charge on one capacitor plus one transistor. It is cheaper and denser than SRAM, but reading it takes longer and the charge must be refreshed periodically.
- Cheap, large memory is much slower still. Flash (SSD) and magnetic disks hold terabytes at low cost. They are also non-volatile, so they keep data without power.
- Large memories are slower even in the same technology. A bigger array has longer wires and more decoding logic, so a 32 KiB SRAM responds faster than a 32 MiB one.
A hierarchy combines several technologies. A small, fast memory sits next to the CPU, and larger, slower memories sit behind it. The goal is to make the whole system behave almost like the fast memory while costing almost as little per bit as the slow one.
The levels
The numbers below are typical orders of magnitude. Exact values change with each processor generation, and GATE questions give you the figures they need.
| Level | Technology | Typical capacity | Typical access time | Managed by |
|---|---|---|---|---|
| Registers | Flip-flop arrays in the CPU | 100s of bytes to a few KB | Under 1 ns (within a cycle) | Compiler |
| L1 cache | SRAM | 32 to 64 KiB | About 1 ns | Hardware |
| L2 / L3 cache | SRAM | 256 KiB to tens of MiB | 3 to 40 ns | Hardware |
| Main memory | DRAM | Gigabytes | 50 to 100 ns | Operating system |
| Secondary storage | SSD or HDD | Hundreds of GB to TB | SSD about 100 µs, HDD 5 to 10 ms | Operating system |
Moving down the table:
- Capacity grows by a factor of 10 to 1000 per step.
- Access time grows by a similar factor.
- Cost per bit falls.
At 3 GHz one clock cycle lasts about 0.33 ns, so a 60 ns DRAM access costs roughly 180 cycles. A CPU that waited for DRAM on every instruction would sit idle most of the time.
How the levels cooperate
Four rules describe the working model.
- The CPU talks only to the top. It reads registers directly and issues loads and stores that go to the nearest cache. It never addresses a disk.
- Each level holds a subset of the level below. The cache holds a copy of some blocks of main memory. Main memory holds some pages of the data on disk. This is the inclusion property. In many designs, though not all, every item in the upper level also exists in the lower one.
- Data moves in blocks, not single bytes. When a needed item is missing from a level, the whole block containing it is copied up. Typical block sizes are 64 bytes between cache and DRAM, and 4 KiB pages between DRAM and disk. Transferring a block amortizes the large startup cost of reaching a slow level.
- Only adjacent levels exchange data. A block moves from disk to DRAM, then from DRAM to cache. It does not skip a level.
When the requested item is found in a level, that is a hit. When it is not found, that is a miss, and the request goes one level down. The next chapters define the ratios and formulas for hits and misses. For now it is enough to know that a high hit rate in the fast levels is what makes the hierarchy worthwhile.
Why it works at all
The hierarchy helps only because programs do not touch memory randomly. They reuse the same data soon (temporal locality) and use data near what they just used (spatial locality). The next chapter treats both in detail. Without locality, almost every access would miss in the small levels, and the hierarchy would add delay instead of removing it.
A quick cost calculation
Suppose a machine has these illustrative prices. They are not real market data.
# (name, capacity in GiB, price in dollars per GiB)
levels = [
("cache (1 MiB SRAM)", 1 / 1024, 10000.0),
("DRAM (16 GiB)", 16, 5.0),
("SSD (512 GiB)", 512, 0.10),
]
total_gib = sum(size for _, size, _ in levels)
total_cost = sum(size * price for _, size, price in levels)
for name, size, price in levels:
share = 100 * size * price / total_cost
print(f"{name:20s} cost ${size * price:8.2f} ({share:4.1f}% of total)")
print(f"Average cost per GiB: ${total_cost / total_gib:.3f}")
print(f"If all memory were SRAM: ${total_gib * 10000.0:,.0f}")The total capacity is about 528 GiB and the total cost is about $141. That is an average of roughly $0.27 per GiB, even though the cache alone costs $10,000 per GiB. Building all 528 GiB from SRAM at that price would cost over $5 million. The small fast level adds only about 7% to the bill, yet it serves most of the CPU's requests. That trade is the reason hierarchies exist.
Common errors
- Treating "cache" as one thing. Modern CPUs have L1, L2 and often L3 caches. L1 is the smallest and fastest, and it is often split into separate instruction and data caches.
- Confusing speed with capacity order. Speed decreases and capacity increases as you move away from the CPU. A question that asks you to "arrange from fastest to slowest" expects registers, cache, main memory, then secondary storage.
- Calling disk a cache for the CPU. The CPU never reads disk directly. The disk is the backing store that main memory caches.
- Mixing units. Access times appear in ns, µs and ms. Capacities appear in KiB, MiB and GiB. Convert everything to one unit before comparing, and remember that 1 KiB = 1024 bytes in GATE problems.
- Assuming a miss means an error. A miss is a normal event. It costs time, not correctness, because the data is fetched from the lower level.
- Forgetting volatility. Registers, caches and DRAM lose their contents when power is removed. Only secondary storage persists.
What GATE expects from this topic
This chapter alone rarely produces a numeric question, but it supports almost every later one. You should be able to:
- Order the levels by speed, size and cost per bit.
- State the inclusion property and the fact that data moves in blocks between adjacent levels.
- Explain in one or two sentences why a hierarchy beats a single large memory.
Remember the ordering, since a typical one-mark question asks which level is fastest, smallest or most expensive per bit. The numeric work on mapping, tag bits and access time starts in the chapters that follow.
Exercise
A 2 GHz processor has a main-memory access time of 50 ns. About how many clock cycles does one main-memory access take, and what does this suggest about the CPU waiting on DRAM directly?
Show answer
One cycle at 2 GHz is 0.5 ns, so 50 ns is 50 / 0.5 = 100 cycles. If the CPU waited for DRAM on every access, it would stall for about 100 cycles each time, so a small fast cache is needed to avoid that wait.
Exercise
Why not build the whole main memory out of SRAM, which is faster than DRAM?
Show answer
SRAM costs far more per bit, uses more chip area and power per bit, and becomes slower as the array grows. A 16 GiB SRAM memory would be very expensive and would not be as fast as a small SRAM cache. A hierarchy uses a little SRAM for speed and a lot of cheap DRAM for capacity.
Exercise
State two properties that hold between a cache and the main memory behind it.
Show answer
First, the cache holds copies of a subset of main-memory blocks (inclusion). Second, data moves between them in whole blocks rather than single bytes, and only between these adjacent levels.