Define Locality of Reference

Define Locality of Reference

Verified Sources
Nov 3, 2026

In computer systems, locality of reference (also called the principle of locality), is the observed behavior that a processor is more likely to access certain memory locations again soon or access nearby locations after an access. This matters because caches and other performance optimizations rely on predicting which data/instructions will be needed next. A standard split is into temporal locality and spatial locality.2

Footnotes

  1. Locality of reference - Wikipedia - Defines locality of reference and temporal/spatial locality. ↩

  2. L14: The Memory Hierarchy - Explains locality as a basis for cache hit prediction and nearby-address expectations. ↩

Locality of Reference in Cache Memory (Spatial vs Temporal)

Core definition (what it means)

Locality of reference is the tendency of a program (during execution) to reference the same memory locations repeatedly over a short period, rather than accessing memory uniformly at random. Informally:

  • If you access address xx at time tt, then you’re likely to access xx again at a later time soon (temporal reuse).
  • If you access address xx at time tt, you’re likely to access addresses near xx soon (spatial reuse), since neighboring elements are often fetched together into cache line/block structures.

Because caches move data/instructions from slower main memory into faster storage in blocks, spatial locality implies that once one element is fetched, nearby elements are also likely to be available without additional long-latency memory accesses.

Footnotes

  1. Locality of reference - Wikipedia - Defines locality of reference and temporal/spatial locality. ↩ ↩2 ↩3 ↩4

Types you should be able to state precisely

  1. Temporal locality
    Reuse of the same data/instructions within a relatively small time window (e.g., loop variables repeatedly used across iterations).2

  2. Spatial locality
    Use of data elements in relatively close memory locations (e.g., array traversal).2

  3. Sequential locality
    A special case of spatial locality: linear/stride-1 access patterns like walking an array from index 0 to n−1n-1.

  4. Locality principle
    Designers exploit locality to make the memory hierarchy efficient: upper levels of the hierarchy are smaller/faster, so good locality keeps relevant blocks there longer and avoids evicting useful data too soon.

Footnotes

  1. Locality of reference - Wikipedia - Defines locality of reference and temporal/spatial locality. ↩ ↩2 ↩3 ↩4

  2. 1.7.2 Cache Memory - Locality of reference - Engineering LibreTexts - Separates temporal vs spatial locality and explains cache placement rationale. ↩ ↩2

How to recognize locality of reference in code (temporal vs spatial)

  1. 1
    Step 1

    Look for loops, counters, and frequently reused variables—these indicate temporal locality.

  2. 2
    Step 2

    Look for array accesses with small strides (e.g., iterating i=0..n−1i=0..n-1 or walking structures in order)—these indicate spatial locality.

  3. 3
    Step 3

    Cache fills typically bring in a block/line that contains the referenced address and nearby bytes/words; spatial locality increases the chance those neighboring elements are used before eviction.

    Footnotes

    1. Locality of reference - Wikipedia - Defines locality of reference and temporal/spatial locality. ↩

  4. 4
    Step 4

    Conditional code can still show locality if the program revisits the same hot paths/working sets repeatedly (phases). Working-set thinking formalizes that locality occurs in phases.

    Footnotes

    1. WORKING SET - the denning institute - Discusses Denning’s working set view: references cluster into phases/locality sets. ↩

  5. 5
    Step 5

    If temporal + spatial locality are strong, you expect higher hit rates and fewer expensive misses; if accesses are random/irregular, you expect more misses.

    Footnotes

    1. L14: The Memory Hierarchy - Explains locality as a basis for cache hit prediction and nearby-address expectations. ↩

Why locality is central to memory performance

Caches work because locality makes it likely that data requested now will be requested again soon (temporal) and that nearby data will already be present after a fill (spatial). In the memory hierarchy, upper levels (caches, registers) are faster but smaller; good locality helps ensure the needed working data stays in those small fast levels rather than being constantly evicted.

Key takeaway: locality of reference is not just a definition—it is an empirical property that allows systems to predict and reduce average memory access time.2

Footnotes

  1. Locality of reference - Wikipedia - Defines locality of reference and temporal/spatial locality. ↩ ↩2 ↩3

  2. L14: The Memory Hierarchy - Explains locality as a basis for cache hit prediction and nearby-address expectations. ↩

Pro Tip: Use locality as a debugging lens

When performance is poor, ask: “Is my code revisiting the same addresses soon (temporal) or scanning nearby memory (spatial)?” Poor locality often looks like cache misses and high latency.2

Footnotes

  1. L14: The Memory Hierarchy - Explains locality as a basis for cache hit prediction and nearby-address expectations. ↩

  2. Locality of reference - Wikipedia - Defines locality of reference and temporal/spatial locality. ↩

Warning: “Random access” may break the assumption

Locality of reference is probabilistic, not guaranteed. Irregular access patterns (e.g., pointer-chasing with unpredictable targets) can significantly weaken spatial/temporal locality, lowering cache effectiveness.

Footnotes

  1. Locality of reference - Wikipedia - Defines locality of reference and temporal/spatial locality. ↩

From locality observation to system design

Working-set thinking

1960s

Denning’s working set model formalizes locality as dynamically clustered references to a subset over time, occurring in phases and transitions."

Footnotes

  1. WORKING SET - the denning institute - Discusses Denning’s working set view: references cluster into phases/locality sets. ↩

Exploiting locality in hardware

Cache era

Cache hierarchies assume repeated accesses and nearby reuse to convert slow memory traffic into faster hits.2"

Footnotes

  1. Locality of reference - Wikipedia - Defines locality of reference and temporal/spatial locality. ↩

  2. L14: The Memory Hierarchy - Explains locality as a basis for cache hit prediction and nearby-address expectations. ↩

Program analysis & locality-aware coding

Modern optimization

Compilers and programmers restructure loops/data layouts to improve locality, increasing the fraction of accesses served in upper cache levels."

Footnotes

  1. Locality of reference - Wikipedia - Defines locality of reference and temporal/spatial locality. ↩

Locality patterns → expected cache behavior (qualitative)

Strong locality increases likelihood of cache hits; weak locality increases misses.

Common clarifications

Knowledge Check

Question 1 of 4
Q1Single choice

Which statement best defines locality of reference?