What is Cache line?

The unit in which data moves between a cache and the level below, also called a cache block. Many CPUs use 64 bytes, and the low address bits give the position (offset) within the line.

Field: Memory Hierarchy

Japanese name: キャッシュライン

Why caches move whole lines

Programs that touch one address usually touch its neighbors soon after, a property called spatial locality. Caches exploit it by moving data in fixed blocks, typically 64 bytes, rather than single words, and the size conveniently matches one DRAM burst: eight transfers of 8 bytes on a 64-bit DDR4 channel. Each cached line carries a tag identifying its address, a valid bit marking real data, a dirty bit in write-back caches and replacement and coherence state. An address therefore splits into tag, index and offset fields; with 64-byte lines, the lowest 6 bits are the offset.

Line size and access patterns

Line size turns access patterns into miss rates. Reading an array of 4-byte integers in order misses once per 16 elements, a 6.25% miss rate, because each miss brings in the next 15 elements. Reading only every 16th element misses every time. C stores two-dimensional arrays in row-major order, so looping over the column index in the inner loop walks consecutive addresses, while swapping the loops jumps a full row per access and may evict lines before they are reused. Larger lines help sequential code but waste bandwidth and capacity when little of each line is used.

Coherence works per line

In a multicore processor, coherence protocols track ownership at line granularity. Under MESI, each line is Modified (changed and held only here, so memory is stale), Exclusive (clean and held only here), Shared (clean copies may exist elsewhere) or Invalid. A core that wants to write must first invalidate copies in other caches; a line already in M can be written repeatedly without notifying anyone. Small systems broadcast these requests, while large ones use directories that record which caches hold each line, so traffic scales with the number of cores.

False sharing

Because coherence is per line, two threads writing different variables that happen to share a 64-byte line still invalidate each other's copies on every write. The line bounces between cores, results stay correct, but performance can drop sharply. Arrays of per-thread counters or flags are the classic case. The fix is to pad or align each thread's data to its own line, for example with alignment attributes in C and C++. Contrast this with true sharing, where threads really do access the same variable and communication cannot be avoided by layout.

Try it: questions about Cache line

What is the typical size of a cache line (cache block) in many CPUs?

  1. A1 byte
  2. B64 bytes
  3. C64 MB
Show answer and explanation

Answer: B. 64 bytes

Most CPUs transfer data between cache and main memory in 64-byte lines. Neighboring addresses are often used together (spatial locality), so fetching them as a block is efficient, and it matches DRAM bursts (8 transfers × 8 bytes = 64 bytes in DDR4). In multithreaded code, beware of false sharing, where separate variables in the same line cause cores to contend for it.

What is the role of the valid bit attached to each cache line?

  1. AIt shows whether the line has been modified
  2. BIt shows the order in which the line will be evicted
  3. CIt shows if real data is present
Show answer and explanation

Answer: C. It shows if real data is present

Right after power-up or after a line is invalidated, cache contents are meaningless. A line whose valid bit is 0 is not counted as a hit even if its tag happens to match; the bit is set to 1 when correct data is loaded. This prevents empty lines from being mistaken for hits. Modification is shown by the dirty bit used in write-back caches, and eviction order is tracked by replacement information such as LRU state.

FAQ

How big is a cache line?

64 bytes on most CPUs, though some processors use 32 or 128 bytes; the size is a hardware property that software can query.

What is the difference between a cache line and a page?

A cache line, usually 64 bytes, is the unit moved between cache levels and memory, while a page, often 4 KB, is the unit of virtual-memory mapping managed by the OS and MMU.

How do I avoid false sharing?

Place data written by different threads in separate cache lines, typically by padding or aligning per-thread structures to 64 bytes.

Questions that mention Cache line (9)

Cache line appears in 11 practice questions across 5 topics, including Multicore & Synchronization, Memory Hierarchy, CPUs and architecture.

  1. In the loop above, an int is 4 bytes, a cache line is 64 bytes, and the array is not cached and starts on a line boundary. Without prefetching, what is the miss rate for reading the array?
  2. In the MESI protocol, what does it mean when a cache line is in the Modified (M) state?
  3. Consider a circuit that adds tamper detection and replay protection to memory encryption. For every 64-byte cache line, an 8-byte MAC (a code for detecting tampering) and an 8-byte counter that increases on every write are stored in main memory. Ignoring the tree that protects the counters, what is the extra storage needed as a proportion of the amount of data protected?
  4. Which correctly describes a DRAM burst transfer?
  5. In a multicore CPU, which mechanism keeps data consistent when the caches of several cores hold data for the same address?
  6. Scanning the 2D C array a[i][j] with j in the inner loop, as above, tends to be faster than with i in the inner loop. Why?
  7. Which correctly lists the four states of the MESI cache coherence protocol?
  8. Which describes false sharing in a multithreaded program?
  9. Which is NOT a reason a parallel program fails to scale as cores are added?

Books on Memory Hierarchy Ad

As an Amazon Associate I earn from qualifying purchases.

Comparisons that include this term

Semiconductor, Electronics and Computing Calculators

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary