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

Locality of Reference

Temporal and spatial locality principles that make the memory hierarchy effective.

Locality of Reference

What You'll Learn

You'll discover the two principles that explain why programs naturally work well with the memory hierarchy you just learned about.

The Core Idea

Locality of Reference is the observation that programs tend to access a relatively small portion of their address space at any given time. This isn't random luck—it's a natural consequence of how we write and execute code.

There are two types of locality:

Temporal Locality

"If I used it once, I'll probably use it again soon."

When your program accesses a memory location, it's likely to access that same location again in the near future. Think of a loop counter variable that gets read and updated repeatedly, or a function that's called many times.

Analogy: Your favorite coffee mug. Once you use it in the morning, you'll probably reach for that same mug again later in the day—not a random one from the back of the cupboard.

Spatial Locality

"If I used this, I'll probably use something nearby next."

When your program accesses a memory location, it's likely to access nearby locations soon after. Arrays are the classic example: processing array[0], then array[1], then array[2] means accessing consecutive memory addresses.

Analogy: Reading a book. When you finish one page, you almost always turn to the very next page—not a random page elsewhere.

Why This Matters

These principles make the memory hierarchy effective. Caches can keep recently-used data (temporal) and fetch chunks of nearby data (spatial), dramatically improving performance without requiring all data to live in expensive, fast memory.

Key Takeaway: Programs exhibit temporal locality (reusing the same data) and spatial locality (accessing nearby data), making multi-level memory hierarchies with small, fast caches highly effective.