Locality of reference refers to a phenomenon in which a computer program tends to access same set of memory locations for a particular time period. In other words, Locality of Reference refers to the tendency of the computer program to access instructions whose addresses are near one another. The property of locality of reference is mainly shown by loops and subroutine calls in a program.
- In case of loops in program control processing unit repeatedly refers to the set of instructions that constitute the loop.
- In case of subroutine calls, everytime the set of instructions are fetched from memory.
- References to data items also get localized that means same data item is referenced again and again.
In above figure, you can see that CPU wants to read or fetch the data or instruction.First it will access the cache memory as it is near to it and provides very fast access. If the required data or instruction is found, it will be fetched. This situation is known as cache hit. But if the required data or instruction is not found in the cache memory then this situation is known as cache miss.Now the main memory will be searched for the required data or instruction that was being searched and if found will go through one of the two ways:
- First way is that the CPU should fetch the required data or instruction and use it and that’s it but what, when the same data or instruction is required again.CPU again has to access same main memory location for it and we already know that main memory is the slowest to access.
- The second way is to store the data or instruction in the cache memory so that if it is needed soon again in near future it could be fetched in a much faster way.
It is based on the principle of locality of reference. There are two ways with which data or instruction is fetched from main memory and get stored in cache memory. These two ways are following:
- Temporal Locality –
Temporal locality means current data or instruction that is being fetched may be needed soon. So we should store that data or instruction in the cache memory so that we can avoid again searching in main memory for the same data.
When CPU accesses the current main memory location for reading required data or instruction, it also get stored in cache memory which is based on the fact that same data or instruction may be needed in near future. This is known as temporal locality.
- Spatial Locality –
Spatial locality means instruction or data near to the current memory location that is being fetched, may be needed soon in near future.This is slightly different from temporal locality.Here we are taking about nearly located memory locations while in temporal locality we were taking about the actual memory location that were being fetched.
The performance of the cache is measured in terms of hit ratio. When CPU refers to memory and find the data or instruction within the Cache Memory, it is known as cache hit. If the desired data or instruction is not found in cache memory and CPU refers to the main memory to find that data or instruction, it is known as cache miss.
Hit + Miss = Total CPU Reference Hit Ratio(h) = Hit / (Hit+Miss)
Average access time of any memory system consists of two levels: Cache and Main Memory. If Tc is time to access cache memory and Tm is the time to access main memory then we can write:
Tavg = Average time to access memory Tavg = h*Tc + (1-h)*(Tm+Tc)
- Computer Organization | Locality and Cache friendly code
- Computer Organization | Cache Memory
- Computer Organization | Micro-Operation
- Cache Organization | Set 1 (Introduction)
- Computer Organization | General Register based CPU Organization
- Computer Organization | Stack based CPU Organization
- Computer Network | DNS Spoofing or DNS Cache poisoning
- Computer Organization | Basic Computer Instructions
- Computer Organization | Performance of Computer
- Computer Organization | RAM vs ROM
- Computer Organization | MPU Communication
- Computer Organization | BUS Arbitration
- Computer Organization | Amdahl's law and its proof
- Computer Organization | Von Neumann architecture
- Computer Organization | Register Allocation
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.