How to implement LRU caching scheme? What data structures should be used?
We are given total possible page numbers that can be referred. We are also given cache (or memory) size (Number of page frames that cache can hold at a time). The LRU caching scheme is to remove the least recently used frame when the cache is full and a new page is referenced which is not there in cache. Please see the Galvin book for more details (see the LRU page replacement slide here).
We use two data structures to implement an LRU Cache.
- Queue which is implemented using a doubly linked list. The maximum size of the queue will be equal to the total number of frames available (cache size). The most recently used pages will be near front end and least recently pages will be near the rear end.
- A Hash with page number as key and address of the corresponding queue node as value.
When a page is referenced, the required page may be in the memory. If it is in the memory, we need to detach the node of the list and bring it to the front of the queue.
If the required page is not in memory, we bring that in memory. In simple words, we add a new node to the front of the queue and update the corresponding node address in the hash. If the queue is full, i.e. all the frames are full, we remove a node from the rear of the queue, and add the new node to the front of the queue.
Example – Consider the following reference string :
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
Find the number of page faults using least recently used (LRU) page replacement algorithm with 3 page frames.
Note: Initially no page is in the memory.
C++ using STL
5 4 1 3
Java Implementation using LinkedHashMap.
The idea is to use a LinkedHashSet that maintains insertion order of elements. This way implementation becomes short and easy.
3 1 4 5
This article is compiled by Aashish Barnwal and reviewed by GeeksforGeeks team. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Least Frequently Used (LFU) Cache Implementation
- Locality of Reference and Cache Operation in Cache Memory
- Cache Memory Design
- Types of Cache Misses
- Multilevel Cache Organisation
- DNS Spoofing or DNS Cache poisoning
- How to Implement Forward DNS Look Up Cache?
- How to Implement Reverse DNS Look Up Cache?
- Cache Memory in Computer Organization
- Differences between Associative and Cache Memory
- Computer Organization | Locality and Cache friendly code
- Difference between Virtual memory and Cache memory
- Write Through and Write Back in Cache
- B*-Trees implementation in C++
- Implementation of Locking in DBMS