Locality of reference refers to the tendency of computer programs to access the same or nearby memory locations repeatedly over a short period of time. This principle is based on the observation that when a program accesses a particular memory location, it is likely to access nearby locations in the near future.
There are two main types of locality of reference:
- Spatial Locality: This refers to the tendency of a program to access memory locations that are close to each other. For example, when a program accesses a particular memory address, it often accesses nearby addresses as well.
- Temporal Locality: This refers to the tendency of a program to access the same memory locations repeatedly over a short period of time. For example, if a program reads a value from a memory location, it is likely to read the same value again in the near future.
Example :
- Take the example of an operating system. Ideally, we would like an unlimited amount of main memory, instantly accessible.
- In practice, we have a limited amount of main memory, and because it is cheaper, a very large amount of secondary memory.
- However the trade-off is that secondary memory tends to be several orders of magnitude slower than primary memory.
- We can approach the ideal by keeping the more often used data in main memory, and everything else in secondary memory.
- Because of the principle of Locality of Reference, we can be sure that most memory references will be to locations already stored in main memory, thereby improving efficiency and providing a flat memory model.
- This scheme is used in modern operating systems and is called virtual memory. Virtual memory gives users the appearance of unlimited primary memory by transparently utilizing secondary memory.
Discussion
Ask a question, share your thoughts or report an error.
Sign in to participate in the discussion.
Join the discussion