Skip to content
BetterDL

Cache memory

Also called: cache, CPU cache, memory cache

A small, fast memory, usually SRAM, placed close to the processor that keeps copies of recently used data so most accesses avoid slower main memory.

Main memory (DRAM) is large but slow compared with a processor. A cache is a small, fast memory, built from SRAM, that sits between them and keeps copies of the data the processor used recently.

It works because programs tend to reuse the same data and to use data near what they just used. So:

  • a hit: the data is already in the cache, and the processor gets it quickly
  • a miss: the data must come from main memory, which takes much longer; a copy is then kept in the cache

The average access time depends on how often you hit:

average time = hit time + miss rate × miss penalty

With a good hit rate, the system feels almost as fast as the cache and almost as big as main memory.

Why SRAM? Speed, and no refresh. Its lower memory density doesn't matter much, because a cache only needs to be small.

Worked example

Example

Average access time

A cache answers in 2 ns. 5% of accesses miss, and a miss costs an extra 60 ns.

  1. 1.

    Average = 2 + 0.05 × 60 = 2 + 3 = 5 ns.

  2. 2.

    Without the cache, every access would take about 60 ns.

  3. 3.

    Halving the miss rate to 2.5% would give 2 + 1.5 = 3.5 ns.

Common mistakes

  • Building a cache from DRAM because it's cheaper. A cache must be fast, so it's SRAM.

  • Thinking the cache replaces main memory. It holds copies of a small part of it.

Practice Cache memory

Interactive questions with instant feedback and a worked solution for every wrong answer.

Learn it step by step

Cache memory is taught in Memory and Basic CPU / Computer Architecture.