A computer system has three levels of memory: Cache, Main Memory, and…
A computer system has three levels of memory: Cache, Main Memory, and Secondary Memory.
Explain how locality of reference helps improve system performance in such a hierarchy. Also explain what would happen to CPU performance if locality did not exist.
Attempted by 147 students.
Show answer & explanation
Memory hierarchy concept
Computer memory is organized in multiple levels: cache, main memory, and secondary memory.
Faster memory is smaller and expensive, while slower memory is larger and cheaper.
Introduction to Memory Hierarchy
Basic hierarchy structure
CPU
│
Cache Memory
│
Main Memory (RAM)
│
Secondary Memory (Disk)Need for locality
CPU frequently accesses only a small portion of memory at a time.
If frequently used data stays in cache, access time becomes very small.
Types of locality
Temporal locality
Recently used data is likely to be used again soon.
Example: loop variables.
Spatial locality
Nearby memory locations are likely to be accessed.
Example: accessing elements of an array.
Example
for(i=0;i<100;i++)
{
sum = sum + A[i];
}Explanation
A[0], A[1], A[2] are stored close to each other.
Once A[0] is fetched, nearby values may already be in cache.
This demonstrates spatial locality.
Impact on performance
If data is found in cache → cache hit → very fast access.
If data not found → cache miss → data fetched from slower memory.
If locality did not exist
CPU would constantly access main memory or disk.
Cache would become useless.
Average memory access time would increase drastically.
Conclusion
Locality of reference makes cache memory effective and improves system performance significantly.