Course contentsShow
← Computer Science
Lesson 1120 of 2,87226. Memory Hierarchy and CachingFree lesson

Cache Lines and Blocks

How cache stores data in fixed-size blocks (lines) rather than individual bytes for efficiency.

Cache Lines and Blocks

You'll learn: How caches organize data into fixed-size chunks called lines or blocks instead of storing individual bytes.

Why Not Store Individual Bytes?

When your CPU needs data from main memory, it could fetch just one byte at a time. But remember the principle of locality you learned? If your program accesses one memory address, it's likely to access nearby addresses soon after.

Instead of making many tiny trips to memory, the cache grabs a whole cache line (also called a cache block) — a fixed-size chunk of contiguous bytes, typically 32, 64, or 128 bytes.

The Bookshelf Analogy

Imagine you need one page from a library book. Rather than walking to the library every time you need the next page, you check out the entire book and keep it on your desk. That book is like a cache line — you fetched more than you immediately needed, but now related data is readily available.

How It Works

When the CPU requests data at a specific memory address:

  1. The cache system identifies which cache line that address belongs to
  2. On a cache miss, the entire line (e.g., 64 consecutive bytes) is fetched from main memory
  3. The whole line is stored in cache
  4. Future accesses to any byte in that line become cache hits

This works brilliantly for programs with good spatial locality — accessing nearby memory locations sequentially (like iterating through an array).

The Efficiency Gain

Fetching 64 bytes as one block is much faster than making 64 separate single-byte requests. The overhead of memory access is paid once, and you get 63 "bonus" bytes that will likely be needed soon.

Key Takeaway: Caches store data in fixed-size lines/blocks (typically 32-128 bytes) rather than individual bytes, exploiting spatial locality to reduce the number of expensive main memory accesses.