Why it is needed

A CPU advances one operation in less than a nanosecond, but fetching data from DRAM main memory takes tens of nanoseconds or more. Waiting for DRAM every time would waste the speed of the CPU. Programs tend to reuse data they used recently and data near it (locality). A small, fast memory therefore keeps copies of recently used data so that most accesses finish there. That memory is the cache.

Why there are levels

Memory cannot be both very fast and very large, so the cache is split into levels. L1, closest to the CPU, is only tens of kilobytes, is divided into instruction and data caches, and is read in a few cycles. L2 is hundreds of kilobytes to a few megabytes and takes a dozen or so cycles. L3 is a few to tens of megabytes, takes tens of cycles, and in most CPUs is shared by several cores. A lookup tries L1, then L2, then L3, and finally main memory.

Hits, misses and average access time

If the wanted data is in the cache it is a hit; otherwise it is a miss, and fetching from the next level costs extra time, the miss penalty. The average access time can be estimated as hit time + miss rate × miss penalty. With a 1 ns hit time and a 100 ns miss penalty, a 5% miss rate gives 6 ns and a 2% miss rate gives 3 ns. A small change in miss rate changes the average speed a great deal. Data moves in lines of, for example, 64 bytes, so using nearby data in sequence makes hits more likely.

Try it: questions about Cache memory

What is the main purpose of cache memory?

  1. ATo retain data when the power is turned off
  2. BTo keep frequently used data close to the CPU and bridge the speed gap with main memory
  3. CTo correct errors in computation results
Show answer and explanation

Answer: B. To keep frequently used data close to the CPU and bridge the speed gap with main memory

While one clock cycle of a CPU running at several GHz lasts 0.2–0.3 ns, a DRAM access takes tens to 100 ns—hundreds of cycles. Frequently used data and instructions are therefore kept in fast SRAM caches to cut waiting. Caches exploit temporal locality (reusing the same data soon) and spatial locality (using nearby addresses next), with L1 reachable in a few cycles and even L3 in tens of cycles.

FAQ

Why is cache memory built from SRAM?

SRAM reads and writes faster than DRAM, needs no refresh and can be built on the same chip with the same process as the CPU. Its area per bit is large, so it is not suited to a large main memory.

Is there anything a programmer can do?

Yes. Walking arrays in order and keeping data that is used together close together make hits more likely. Traversing a two-dimensional array against its layout in memory can add many misses and slow a program considerably.

Books on CPUs and architecture 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