Memory Hierarchy
Organize RAM and ROM, map cache lines, calculate effective access time, translate virtual addresses, and compare replacement and write policies.
Updated
Learning objectives
- Explain why locality makes a memory hierarchy effective
- Construct memory modules and address maps from RAM and ROM chips
- Derive direct, associative, and set-associative cache fields
- Calculate hit ratio, average access time, speed ratio, and efficiency
- Trace virtual-address translation, page faults, and replacement policies
Prerequisites
- Computer Organization
- Digital Components
A useful memory system must appear large, fast, and inexpensive, but no single technology maximizes all three. A hierarchy keeps the current working set in small fast levels and the rest in progressively larger, slower levels.
| Term | Definition |
|---|---|
| Latency | Time from a request until its requested item becomes usable. |
| Bandwidth | Amount of information transferred per unit time. |
| Cache line | Fixed-size block moved between a cache and the next memory level. |
| Page | Fixed-size virtual-memory block mapped to a physical frame. |
| Hit | The requested item is present in the level being checked. |
| Miss penalty | Extra time needed to obtain the item from a lower level. |
| Level | Typical role | Volatile? | Unit transferred |
|---|---|---|---|
| CPU registers | Immediate operands and state | Yes | Word |
| L1/L2/L3 cache | Recently used instructions and data | Yes | Cache line |
| Main memory | Active processes | Yes | Cache line or page |
| SSD/disk | Files and virtual-memory backing | No | Page/block |
| Archive | Long-term backup | No | Large blocks |
Why the Hierarchy Works
Programs show temporal locality—recently used items tend to be reused—and spatial locality—nearby addresses tend to be used soon. Loops, stacks, arrays, instructions in a basic block, subroutines, and symbol tables all exhibit locality. Moving an entire block on a miss anticipates nearby references.
The terms hit and miss are relative to a level. Hit ratio is the fraction found there; miss rate is . Miss penalty includes the time to fetch from the next level and make the requested word available.
Main Memory Technologies
RAM
Random-access memory takes approximately the same time to access any addressed word.
| Type | Cell | Refresh | Density/cost | Common use |
|---|---|---|---|---|
| SRAM | Bistable latch, commonly six transistors | No | Lower density, expensive | Cache |
| DRAM | Capacitor plus access transistor | Required | High density, inexpensive | Main memory |
DRAM reads are destructive and must restore the cell. Its address may be multiplexed into row and column portions to reduce package pins. SRAM is faster and simpler to interface but consumes more area per bit.
ROM family
| Type | How contents change |
|---|---|
| Mask ROM | Fixed during manufacture |
| PROM | Programmed once |
| EPROM | Erased with ultraviolet light and reprogrammed |
| EEPROM/flash | Electrically erased and reprogrammed |
ROM stores firmware and fixed lookup data. Modern flash is nonvolatile and rewritable, but writes are slower and organized in erase blocks.
Building and Addressing a Memory Module
An chip stores words of bits. It needs address inputs and data connections. Capacity expansion has two independent directions:
- Put chips in parallel to widen each word.
- Decode additional high-order address bits to select one bank and increase the number of words.
For example, build from chips. Each chip uses nine address bits because . Four chips provide depth; go to every chip and a 2-to-4 decoder driven by selects one chip.
If four 512-byte regions start at address 0x000, their inclusive ranges are:
| Select | Binary high bits | Address range |
|---|---|---|
CS0 | 00 | 0x000–0x1FF |
CS1 | 01 | 0x200–0x3FF |
CS2 | 10 | 0x400–0x5FF |
CS3 | 11 | 0x600–0x7FF |
The same decoder can select a mixture of RAM and ROM, creating a memory map. Read/write control must be disabled for ROM.
Cache Organization
A cache line contains a valid bit, a tag, a data block, and often dirty/replacement metadata. The CPU address separates into:
Direct mapping
Main-memory block maps to cache line , where is the number of cache lines. The index selects one line and its stored tag confirms whether that line contains the requested block. Hardware is small and fast, but two active blocks with the same index repeatedly evict each other.
Source example: a main memory has words and a one-word-line cache has lines. Its 15-bit address splits into a 6-bit tag and a 9-bit line index. If each line instead contains eight words and the cache still holds 512 words, it has lines, so the split is 6 tag bits, 6 line bits, and 3 word bits.
Fully associative mapping
Any memory block may occupy any line. Every stored tag is compared in parallel, eliminating conflict based solely on index. The parallel comparison and replacement search cost more hardware.
Set-associative mapping
The cache is divided into sets with ways per set. Block maps to set and may occupy any of its ways. It balances direct mapping’s fast indexing with associative placement inside a small set.
For a 32 KiB, 4-way cache with 64-byte lines and 32-bit byte addresses:
Offset bits , set bits , and tag bits .
Cache Lookup, Replacement, and Writes
Common victim policies are:
- FIFO: replace the line resident longest; simple, but not sensitive to actual use.
- LRU: replace the least recently referenced line; good locality behavior, but exact tracking becomes costly at high associativity.
- Random/pseudo-LRU: inexpensive approximations often used in hardware.
- Optimal: replace the item whose next use is farthest in the future; unattainable online, but a useful lower bound.
On a write, write-through updates cache and main memory immediately; a write buffer can hide some delay. Write-back updates only the cache, marks the line dirty, and writes it to memory when evicted. A write miss also needs an allocation choice: write-allocate fetches the line, whereas no-write-allocate writes around the cache.
Average Access Time
If a hit costs and a miss first checks cache and then accesses main memory in , the source model is
With ns, ns, and :
The main-memory/cache speed ratio is . Efficiency relative to ideal cache time is
Always state the timing convention. Some systems overlap cache lookup with the lower-level access and use ; the numerical result happens to be the same algebraically here.
For multiple levels, let be the fraction of all requests that reach level , and its access time:
Using the lecture example’s stated reach fractions and latencies— and —
This calculation treats as unconditional fractions of all references. If hit probabilities are conditional on reaching a level, multiply the preceding miss probabilities to derive instead.
Virtual Memory
Virtual memory gives each process a private address space larger and more regular than available physical RAM. It divides virtual space into fixed-size pages and physical memory into equal-size frames.
For virtual address width , physical address width , and page size bytes:
| Field | Width |
|---|---|
| Page offset | bits |
| Virtual page number | bits |
| Physical frame number | bits |
A page-table entry records the frame plus valid/present, protection, referenced, and dirty information. A translation lookaside buffer (TLB) caches recent page-table entries.
For a 32-bit byte address and a 4 KiB page, the offset has bits and the virtual page number has bits. If physical memory is 1 GiB, physical addresses have 30 bits, so a frame number needs bits. Translation changes the 20-bit virtual page number into an 18-bit frame number; it copies the 12-bit offset unchanged.
On a TLB miss, hardware or software consults the page table. If the page is present, translation is inserted into the TLB. If absent, a page fault transfers control to the operating system, which selects a frame, writes a dirty victim if needed, reads the required page from storage, updates the page table/TLB, and restarts the instruction.
Page Replacement
FIFO, LRU, and Optimal also illustrate virtual-memory replacement:
| Policy | Decision basis | Important property |
|---|---|---|
| FIFO | Oldest loaded page | Can show Belady’s anomaly: more frames can cause more faults |
| LRU | Oldest last use | Stack property; no Belady anomaly |
| Optimal | Farthest future use | Minimum possible faults; requires future knowledge |
For reference string 7 0 1 2 0 3 0 4 and three empty frames, FIFO evolves:
7-- 70- 701 201 201 231 230 430
F F F F H F F F
There are seven faults and one hit. LRU differs at the request for 3: because 1 is least recently used, it evicts 1 rather than FIFO’s 0. Optimal is useful for comparing algorithms, not implementing a real system.
Check Your Understanding
- Why is a complete block fetched on a cache miss rather than only one word?
- Split a 24-bit byte address for a direct-mapped 4 KiB cache with 16-byte lines.
- What is the average time for ns, ns, and under the sequential-lookup model?
- Which virtual-address field passes through translation unchanged?
Answers
- Spatial locality makes nearby words likely to be referenced soon.
- , so 4 offset bits; lines, so 8 index bits and tag bits.
- ns.
- The page offset.