Implementation of LRU Page Replacement Algorithm in C++
In this post, we implement the LRU (Least Recently Used) Page Replacement Algorithm in C++. When a page fault occurs and all frames are occupied, LRU evicts the page that has not been used for the longest time. Unlike FIFO which only tracks load order, LRU tracks usage history, making it significantly more effective in practice. What is LRU Page Replacement? LRU exploits the principle of temporal locality — recently used pages are likely to be used again soon. By evicting the page that hasn’t been touched for the longest time, LRU keeps the most actively used pages in memory. Page Hit — Referenced page is already in a frame (its last-used time is implicitly updated). Page Fault — Referenced page is not in memory. On a fault with full frames, the frame whose page was accessed least recently is selected for replacement. LRU identification — For each frame, scan the page history backwards from the current reference to find the most recent access of that frame’s page. The frame with the oldest last-access is the LRU victim. LRU does not suffer from Bélady’s Anomaly (unlike FIFO) and closely approximates the optimal (OPT) algorithm.