Memory and caches, in 18 keywords

Learn the keywords, then use them to explain why an array scan is fast and pointer chasing is slow.

Your win today You can explain all 18 keywords in one sentence each. Given a data structure, you can predict whether it will suffer from cache misses, TLB misses or useless prefetching, and say why. That covers the exam questions on MemoryAndCache p.36. About 30–40 minutes.

1 · The chips: SRAM vs DRAM

SRAM static RAM
Each bit is a flip-flop made of several transistors. It keeps its value as long as the chip has power MemoryAndCache pp.6, 8. SRAM is fast but large and expensive per bit, so it is used for the on-chip caches (10s of MB) p.17.
DRAM dynamic RAM
Each bit is a capacitor with 1 transistor. The charge leaks, so DRAM needs periodic refresh. An access selects the row first, then the column pp.6, 8, 12. DRAM is dense and cheap, so it is used for main memory (GBs–TBs). Each access costs ~100–200 cycles.
bank · rank · DIMM
A bank is a grid of rows and columns. A chip holds several banks, which adds capacity. A rank is a set of chips read at the same address together, which adds bandwidth. A DIMM is a memory module with several ranks; a server has up to 16 DIMMs per CPU. One DIMM delivers about 12–25 GB/s with DDR4 p.12. Memory bandwidth grows by reading many chips in parallel, the same idea as SSD channels in 0001.
HBM high bandwidth memory
DRAM stacked in 3D on the same package as the processor, with very wide buses and an access unit of more than 512 bits p.16. Very high bandwidth but modest capacity. It is mostly used on GPUs and accelerators, which are coming up in the next lectures.

Why is SRAM used for caches and DRAM for main memory?

SRAM flip-flops are fast but big; a DRAM cell is one transistor and a capacitor MemoryAndCache pp.8, 17.

What raises bandwidth in a DRAM module?

Banks per chip add capacity; chips in a rank read in parallel add bandwidth p.12.

2 · Virtual memory: the address you use is not the real one

virtual memory
Each process sees its own contiguous address space as if nothing else were running. When you allocate memory, no physical memory is touched until the first access pp.19–20. It makes programming easier and safer, but every memory access now needs a virtual-to-physical translation.
MMU / page table
The MMU (memory management unit, in the CPU hardware) works with the OS to translate addresses one page at a time. The page table is itself stored in memory p.19. Without a shortcut, every load would need extra memory reads just to look up the page table.
TLB translation lookaside buffer
A small associative cache of recent page-table entries, 1000s of entries, usually in several levels. On a TLB miss the CPU has to walk the page table with extra memory reads pp.20–21. Random access spread over many pages misses the TLB, on top of missing the caches.
huge pages 2 MB / 1 GB
Pages much larger than the default 4 KB, so one TLB entry covers far more memory. They are also physically contiguous, which DMA needs p.21. They reduce TLB pressure, at the risk of memory fragmentation. This is the answer to “what do huge pages solve?” on p.36.

A 64 GB hash table is probed at random keys. Which change most directly cuts TLB misses?

With 4 KB pages, 64 GB needs about 16 M translations, far more than the TLB holds. With 1 GB pages it needs 64 p.21.

What happens on a TLB miss?

A TLB miss means extra memory reads to fetch the translation p.20.

3 · Why caches work: locality

temporal locality
Data used recently is likely to be used again soon. Example: sum in a loop, or the loop's own instructions pp.23–24. This is why keeping recently used data close to the core pays off.
spatial locality
Data at nearby addresses tends to be used close together in time. Example: array[i], array[i+1] pp.23–24. This is why caches load a whole block (a cache line) instead of a single byte. Locality is the reason the hierarchy is built the way it is p.23.
cache line 64 B
The unit the cache loads and evicts. Lines are always moved whole, and DRAM is efficient at moving whole blocks like this p.29. Reading one 8 B value costs as much as reading the full 64 B line around it. Project 1 uses 64 B nodes for exactly this reason, so each lookup is one line Exercise_01 p.50.

In for (i…) sum += a[i];, which locality does sum show?

The same variable is reused every iteration ⇒ temporal; a[i] is the spatial part p.24.

You read one 8 B value that misses all caches. How much data enters the cache?

Caches load whole 64 B lines p.29.

4 · How a cache is managed

write-back vs write-through
Write-through sends each write to the next level immediately. Write-back only marks the line dirty and writes it out when it is evicted. CPU caches use write-back; a dirty line is written to DRAM when it leaves the LLC pp.25, 29, 31. Write-back saves bandwidth, but it is the reason other cores need a coherence protocol.
replacement policy / inclusivity
When the cache is full, a policy picks which line to evict. In an inclusive cache, the lower levels usually also hold the data of the higher ones p.25. The slide calls the replacement policy “a crucial optimization”. The same question comes up for any cache: web, DBMS, or the OS page cache p.26.
conflict / cache coloring
To keep lookups constant-time, many memory addresses map to the same one or few cache slots. When those addresses are used together, they evict each other p.30. If you know the cache's geometry, you can lay out data so the addresses don't collide. This is called cache coloring.
hardware prefetching
The hardware loads data before you ask for it. Next-line: a miss on a fetches a+1. Stream: misses on a, a+1 fetch a+2, a+3. Stride: misses on a, a+20 fetch a+40, a+60 p.27. Prefetchers are deliberately simple. They help sequential and strided scans, but not irregular access.
pointer chasing
Following node->next: the next address is only known after the current load finishes pp.27, 34. No prefetcher can guess the next address, so every hop pays the full DRAM latency. Linked lists, chained hash tables and trees all have this problem.

Why doesn’t the stride prefetcher rescue a linked-list traversal?

A prefetcher can only guess from the pattern of past addresses. With pointer chasing, the next address is stored inside the node, so there is no pattern to find p.27.

Accesses go to a, a+20, a+40, … Which prefetcher helps?

Stride prefetching detects a constant distance: miss a, a+20 → fetch a+40, a+60 p.27.

5 · Many cores: coherence and working set

MESI / snooping
Each cache line is tagged Modified, Exclusive, Shared or Invalid. Before reading or writing, a core broadcasts its intention over the interconnect, and the line's state changes both locally and in the other cores' caches. Real CPUs use more states (MOESI, MESIF) p.33. If Core 1 has a dirty line and Core 2 wants to read it, they must coordinate. Writing to a line that is in Exclusive state needs no messages, because no other core has a copy (p.36 T/F).
working set vs LLC
The working set is the data a program touches repeatedly. If it is larger than the last-level cache, most accesses go to DRAM p.34. Fix: process the data in batches that fit in the cache, then combine the results. The extra work is often worth it.

Core 1 holds a line in Modified state; Core 2 reads it. What must happen?

Coherence ensures data is never stale: intentions are broadcast and states change on both cores p.33.

Your working set is 200 MB and the LLC is 32 MB. Which fix do the slides suggest?

Batch, then combine; the extra work can pay off p.34.

Best-practice checklist p.34 Avoid random access across many pages (TLB) → sorted array or huge pages · avoid pointer chasing (no prefetch) → move the “pointed-to” data up into the parent structure · avoid working sets larger than the LLC → batch · measure with perf and hardware performance counters.

6 · Keyword recall drill

Cover the page. For each keyword, say a one-sentence definition and why it matters, then open it to check. Mix in a few keywords from 0001 as well.

SRAMFlip-flop cells, no refresh, fast and expensive. Used for caches.
DRAM1 capacitor per bit, needs refresh, row then column. Used for main memory.
bank · rank · DIMMBanks give capacity; ranks of chips read in parallel give bandwidth.
HBM3D-stacked on the package, wide bus, >512-bit access unit. Used on GPUs.
virtual memoryEach process gets a private contiguous address space, translated on every access.
MMU / page tableHardware plus OS translate addresses; the page table lives in memory.
TLBCache of translations. A miss means extra memory reads for the page walk.
huge pages2 MB–1 GB pages: fewer TLB entries needed, contiguous for DMA, risk of fragmentation.
temporal localityData used recently is used again soon.
spatial localityNearby addresses are used close together in time.
cache line64 B unit of loading and eviction.
write-back / write-throughWrite on eviction (dirty) / write immediately.
replacement policyChooses which line to evict when the cache is full.
conflict / cache coloringAddresses mapping to the same slot evict each other; lay data out to avoid it.
prefetchingNext-line, stream and stride. Helps regular access patterns.
pointer chasingNext address unknown until the load finishes, so prefetching can't help.
MESI / snoopingLine states plus broadcast intentions keep the cores' caches coherent.
working setIf it's larger than the LLC, you're DRAM-bound; batch the work.

7 · True / False

Click True or False. The explanation appears after you answer.

T / FDRAM keeps its content as long as power is on, without any refresh.

False. That describes SRAM. DRAM's capacitors leak and need periodic refresh even while powered MemoryAndCache pp.6, 12.

T / FAllocating 1 GB of memory immediately uses 1 GB of physical DRAM.

False. No physical memory is touched until an address is first accessed p.20.

T / FThe TLB caches data values, just like L1.

False. It caches page-table entries (virtual-to-physical translations) p.20.

T / FHuge pages reduce TLB pressure but can cause memory fragmentation.

True p.21.

T / FA stride prefetcher speeds up linked-list traversal.

False. With pointer chasing there is no address pattern to detect p.27.

T / FA write-back cache sends every store to DRAM immediately.

False. That is write-through. Write-back marks the line dirty and writes it out on eviction pp.25, 29.

T / FCaches load and evict whole 64 B lines, never single bytes.

True p.29.

T / FHBM gives more capacity than DDR DIMMs.

False. HBM has high bandwidth but modest capacity p.16.

T / FTransparent prefetching from SSDs is very effective, so storage systems should rely on it.

False. The cost of a wrong guess is too high. It is better to prefetch from the application (async I/O, pipelining) p.28.

T / FAdding chips in a rank (accessed at the same addresses) increases bandwidth, not latency.

True. A rank is several chips read at the same address in parallel ⇒ more bandwidth p.12.

8 · What is …? no options: answer aloud, then reveal

What isassociative (content-addressable) memory? Give an example.

Show answer

Memory where a location is found by a tag (its content) instead of an address. Examples: TCAMs, and the TLB pp.7, 20.

What isa dirty cache line?

Show answer

A line modified in the cache but not yet written back to DRAM. It is written when evicted from the LLC pp.29, 31.

What iscache inclusivity?

Show answer

The property that lower cache levels (usually) also hold the data of the higher levels p.25.

What isa non-allocating load?

Show answer

A special read instruction that does not bring the line into the cache p.31.

What iscache coloring?

Show answer

Laying out data, using knowledge of the cache's geometry, so that addresses used together don't map to the same cache slot p.30.

What isthe page table, and who uses it?

Show answer

The virtual-to-physical mapping per process. It is stored in memory, managed by the OS and walked by the MMU p.19.

What isstream prefetching?

Show answer

After misses on a and a+1, the hardware fetches a+2 and a+3 p.27.

What isthe meaning of each MESI letter?

Show answer

Modified (dirty, only copy) · Exclusive (clean, only copy) · Shared (clean, possibly other copies) · Invalid p.33.

What isthe list of four places caching appears, from coarse to fine?

Show answer

Web/DBMS result caches → DDR in front of flash → the OS page buffer → CPU caches p.26.

9 · Calculations

ComputeHow many 4 KB pages does a 64 GB table need? How many 1 GB huge pages?

Show solution

64·2³⁰ / 2¹² = 2²⁴ ≈ 16.8 M pages vs 64 huge pages p.21.

ComputeTLB reach: a TLB has 1 536 entries (an assumed number for practice). How much memory can it map with 4 KB pages, and with 2 MB pages?

Show solution

1 536 × 4 KB = 6 MB; 1 536 × 2 MB = 3 GB. Random access over more than 6 MB with 4 KB pages keeps missing the TLB pp.20–21.

ComputeA sequential scan of 10⁶ int64 values without prefetching. How many cache lines are loaded, and what is the miss rate per element?

Show solution

8 MB / 64 B = 125 000 lines. 8 values per line ⇒ 1 miss per 8 accesses = 12.5%. This is spatial locality at work pp.24, 29.

ComputeAverage access time: 90% of accesses hit L1 (4 cy), 7% L2 (10 cy), 2% L3 (40 cy), 1% DRAM (200 cy). What is the average? What if DRAM accesses go up to 5% (taking that share from L1)?

Show solution

0.9·4 + 0.07·10 + 0.02·40 + 0.01·200 = 3.6 + 0.7 + 0.8 + 2 = 7.1 cy. With 86% / 7 / 2 / 5: 3.44 + 0.7 + 0.8 + 10 = 14.9 cy, which is 2× slower from 4% more misses. Latencies are taken from Storage p.6.

ComputeTraversing a linked list of 10⁶ nodes of 64 B each, where every hop is a DRAM miss (80 ns), vs scanning the same 64 MB array at 20 GB/s. How long does each take?

Show solution

List: 10⁶ × 80 ns = 80 ms. Array: 64 MB / 20 GB/s ≈ 3.2 ms ⇒ ~25× faster. Pointer chasing pays latency; a scan pays only bandwidth pp.12, 27, 34.

10 · Exam-style questions (written)

  1. Give an example. Name an application or data structure that suffers from excessive TLB misses, and say why p.36.
  2. Give an example. Name an application or data structure that benefits from prefetching in main memory p.36.
  3. Explain. What issue or issues do huge pages solve, and what new issue can they cause p.36?
  4. True/False + justify. In directory-based cache coherence, a write to a cache line in Exclusive state incurs no coherence messaging overhead p.36.
  5. Compare (Project 1 thinking). A direct-access array and a chained hash table with bin size 1 are both O(1) per lookup. Using the keywords above, predict which has the lower latency per lookup and explain why “O(1) ≠ O(1)” Exercise_01 pp.28, 48. This is a conceptual question only; your lab measurements are your own work.

Bring your answers to the chat and I'll grade them the way an examiner would.

Read next

Primary source: Ulrich Drepper, What Every Programmer Should Know About Memory. Read §2.1 (DRAM), §3.1–3.3 (caches) and §4.3 (TLB). It's long, so only skim those sections. Then use the L02 cheat sheet for review.