Locality of Reference and Cache Operation in Cache Memory
Last Updated :
17 Sep, 2024
This article deals with the concept of locality of reference and cache operations in cache memory. Locality of reference refers to the process of accessing a particular portion of memory at any given time period. Cache memory is a small-sized memory that allows fast access by storing the frequently needed data and instructions. And cache operations are those operations that allow to store data and instructions in cache memory so that fast retrieval is possible.
What is Cache Memory?
Cache memory is a small, high-speed storage area in a computer. The cache is a smaller and faster memory that stores copies of the data from frequently used main memory locations. There are various independent caches in a CPU, which store instructions and data. The most important use of cache memory is that it is used to reduce the average time to access data from the main memory.
By storing this information closer to the CPU, cache memory helps speed up the overall processing time. Cache memory is much faster than the main memory (RAM). When the CPU needs data, it first checks the cache. If the data is there, the CPU can access it quickly. If not, it must fetch the data from the slower main memory.
What is Locality of Reference?
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, every time 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 the above figure, you can see that the 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 a cache hit. But if the required data or instruction is not found in the cache memory then this situation is known as a 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 the 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 the near future it could be fetched in a much faster way.
What is Cache Operation?
Cache operations are those operations that allow fast data retrieval by using principle of locality of reference. They store the nearby or same data in the cache memory if they are frequently accessed by the CPU. There are two ways with which data or instruction is fetched from main memory and get stored in cache memory. These two ways are the 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 gets stored in the 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. If some data is referenced, then there is a high probability that it will be referenced again in the near future.
- Spatial Locality – Spatial locality means instruction or data near to the current memory location that is being fetched, may be needed soon in the near future. This is slightly different from the temporal locality. Here we are talking about nearly located memory locations while in temporal locality we were talking about the actual memory location that was being fetched.

Cache Performance: 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 the cache memory and CPU refers to the main memory to find that data or instruction, it is known as a cache miss.
Hit + Miss = Total CPU Reference
Hit Ratio(h) = Hit / (Hit+Miss)
Miss Ratio = 1 - Hit Ratio(h)
Miss Ratio = Miss / (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
For simultaneous access
Tavg = h * Tc + (1-h)*Tm
For hierarchial access
Tavg = h * Tc + (1-h)*(Tm + Tc)
Conclusion
In conclusion, locality of reference is an important concept that tells us how to utilize the cache memory efficiently in such a way that increases the speed of data retrieval. This is possible due to the cache operations – Temporal locality and Spatial locality which help to store the frequently accessed data in the cache memory. Thus, cache performance increases which can be measured by finding the average memory access time.
Similar Reads
Read and Write operations in Memory
A memory unit stores binary information in groups of bits called words. Data input lines provide the information to be stored into the memory, Data output lines carry the information out from the memory. The control lines Read and write specifies the direction of transfer of data. Basically, in the
3 min read
Cache Hits in Memory Organization
The user has a memory machine. It has one layer for data storage and another layer for the cache. The user has stored an array with length N in the first layer. When the CPU needs data, it immediately checks in cache memory whether it has data or not. If data is present it results in CACHE HITS, els
8 min read
Levels of Memory in Operating System
Memory hierarchy of a computer system it handles differences in speed. "Hierarchy" is a great way to say "order of thinks" like top to bottom, fast to slow, most important to least important. If you look at the memory hierarchy inside the computer, according to the fastest to the slowest: 1. CPU Reg
3 min read
Basic Concepts of Optimizing for Parallelism And Locality
In this article, we'll discuss some of the basic concepts in compiler design that can be used to exploit parallelism and locality. We'll start by looking at how computers work, and then move on to discuss the three types of parallelism available on modern processors: loop-level parallelism, data loc
6 min read
Cache Memory in Computer Organization
Cache memory is a small, high-speed storage area in a computer. The cache is a smaller and faster memory that stores copies of the data from frequently used main memory locations. There are various independent caches in a CPU, which store instructions and data. The most important use of cache memory
11 min read
Printing the Address of an Object of Class in C++
Prerequisite: Classes and Objects in C++ The location of an object in the memory is called its address. Addressing is a necessary part of C++, it enables us to use any element as a reference and maintains the uniqueness of all the elements whether it is any variable, object, or container. In this ar
3 min read
Write Through and Write Back in Cache
Prerequisite - Multilevel Cache Organisation Cache is a technique of storing a copy of data temporarily in rapidly accessible storage memory. Cache stores most recently used words in small memory to increase the speed at which data is accessed. It acts as a buffer between RAM and CPU and thus increa
3 min read
Difference between Cache Memory and Register
In the context of a computerâs architecture, two terms that may be familiar are the cache memory and the register, however, few people may not know the differences and functions of each of the two components. Both of them are important to the CPU but they have different roles and are used in differe
5 min read
Cache Memory Performance
Types of Caches : L1 Cache : Cache built in the CPU itself is known as L1 or Level 1 cache. This type of cache holds most recent data so when, the data is required again so the microprocessor inspects this cache first so it does not need to go through main memory or Level 2 cache. The main significa
5 min read
Difference Between Spatial Locality and Temporal Locality
In computer systems, the concept of locality of reference is used to increase the efficiency of the cache memory by trying to predict the further usage of the data. The locality of reference is divided into two types: the two categories of locality; spatial locality and temporal locality. The distin
5 min read