Not Recently Used (NRU) page replacement algorithm


Operating systems use the Not Recently Used (NRU) page replacement algorithm as a fundamental page replacement tactic to control memory. Its major goal is to locate and remove pages from memory that haven't been accessed in a while.

In this article, we will be discussing the NRU page replacement algorithm, the classes in it, the steps involved, the use cases, and also its benefits.

The NRU algorithm Classes

Based on their usage or reference bit, pages are divided into four classes by the NRU algorithm −

Class 0 − Since they were loaded into memory, pages cannot be referenced (accessed) or modified (written to).

Class 1 − Pages that have undergone modification while being loaded into memory but have not been referenced.

Class 2 − Pages that have been used as references but not changes.

class 3 − Pages that have been changed and cited fall under.

The reference bit of every page is normally periodically reset using the NRU method using a clock or timer device. The method analyzes every page in memory before choosing one to evict on each clock tick or timer interrupt, classifying each page based on its reference and modifying bits.

In NRU, the selection procedure entails looking for the lowest numbered non-empty class and randomly ejecting a page from that class. The method prioritizes evicting pages that have not been used recently by selecting the lowest numbered class, increasing the possibility of evicting less important or less often used sites.

When more complex algorithms like Least Recently Used (LRU) are impractical due to implementation restrictions, particularly in systems with limited resources, NRU is a reasonably basic and effective page replacement mechanism that can be used. It is important to keep in mind that NRU does not take page references into account and that its random eviction policy might not always produce the best performance.

Steps in NRU Algorithm

The following steps form the basis of how the NRU (Not Recently Used) page replacement mechanism works −

  • A reference bit (R) and a modified bit (M) are given to every page in memory. Normally, each page table entry for a page contains these parts.

  • The algorithm starts the NRU selection procedure at regular intervals or in response to specified events (such as clock ticks or timer interrupts).

  • Based on their R and M bits, the algorithm sorts all of the pages in memory into one of the following four classes−

    Class 0 − Pages have R and M values of 0.

    Class 1 − Pages have R = 0 and M = 1.

    Class 2 − ages where R = 1 and M = 0.

    Class 3 − Pages where R and M both equal 1.

  • A page is chosen for eviction by the algorithm after the pages have been classified. It starts by looking for the class with the lowest number that is not empty. The algorithm favors the class with the lesser number of pages when there are numerous classes. For instance, class 0 would be chosen if both class 0 and class 1 had pages.

  • The program picks a page at random for eviction from the designated class. This can be done by selecting a page at random after iterating over all of the pages in the class or by using a random number generator.

  • A replacement page can then be loaded if necessary when the selected page is removed from memory. An appropriate modification is made to the related page table entry.

  • Finally, to get ready for the subsequent selection cycle, all of the memory's remaining pages have their reference bits (R) cleared (set to 0).

NRU tries to delete pages that are less likely to be needed in the near future by routinely scanning and evicting pages that have yet to be utilized (i.e., having a reference bit of 0). NRU does not take into account the frequency of page references, which can lead to a less-than-ideal performance in some circumstances.

Here is the Flow chart for the NRU Algorithm−

Use Cases of NRU Page Replacement Algorithm

Let us explore some of the use cases for non-contiguous allocation in operating systems.

  • Variable-sized processes − Non-contiguous allocation is particularly useful when dealing with processes of varying sizes. It allows memory to be dispersed among several regions, enabling efficient utilization of available memory.

  • Dynamic memory management − Non-contiguous allocation enables dynamic memory management, allowing memory to be allocated and deallocated in response to the initiation or termination of activities. This flexibility is essential in environments where processes have changing memory requirements.

  • Effective memory utilization − Non-contiguous allocation ensures efficient memory utilization by allocating memory blocks according to the actual needs of the processes. It prevents wasting memory by allocating only the necessary amount of memory for each process.

  • Fragmentation management − While non-contiguous allocation can increase external fragmentation, it helps reduce internal fragmentation. Internal fragmentation occurs when allocated memory blocks have empty or partially utilized space. By allocating memory blocks based on the specific needs of processes, internal fragmentation is minimized.

  • Handling large datasets − Non-contiguous allocation is beneficial when dealing with large datasets that cannot fit into a single contiguous memory block. The ability to allocate memory in multiple non-contiguous blocks allows processes to work with extensive data without exhausting the available memory resources.

  • Virtual memory systems − Non-contiguous allocation is a fundamental technique used in virtual memory systems. It allows the operating system to map logical addresses to physical addresses in a flexible manner, enabling processes to access memory in a non-contiguous fashion.

  • Multi-programming environments − In multi-programming environments where multiple processes are running concurrently, non-contiguous allocation helps allocate memory blocks to different processes efficiently. Each process can be assigned memory based on its specific requirements, allowing for optimal memory usage.

  • Real-time systems − Non-contiguous allocation can be useful in real-time systems where processes have stringent timing requirements. It allows memory to be allocated and deallocated dynamically, enabling processes to efficiently manage their memory resources while meeting strict timing constraints.

    By employing non-contiguous allocation, operating systems can cater to the diverse memory needs of processes, promote efficient memory utilization, and provide the flexibility required to handle varying workloads and data sizes.

Benefits of NRU Page Replacement Algorithm

In some circumstances, the NRU (Not Recently Used) page replacement mechanism offers a number of benefits. Here are a few NRU algorithm benefits−

Simplicity − NRU is a relatively easy-to-implement page replacement method. The simplicity of NRU, for instance, may assist embedded systems or older hardware with memory restrictions.

Favoring Rarely Visited Pages − NRU gives priority to evicting rarely visited pages. NRU can help ensure that less often used pages are removed first, making space for more actively utilized pages, on a system where a particular application or data set is used infrequently yet demands a significant amount of memory.

Quick Adaptability to Changing Access Patterns − Rapid adaptability to shifting access patterns is made possible by NRU, which offers a straightforward technique for doing so. This enables the algorithm to modify, albeit coarsely, its eviction decisions depending on the most recent access patterns.

Randomized Eviction − To choose which page to evict from each class, NRU uses a random selection technique. Additionally, it can make evictions less predictable, making it more difficult for malicious software to take advantage of particular eviction patterns.

Conclusion

The NRU (Not Recently Used) page replacement technique is straightforward, requires little overhead, and allows for the prioritization of evicting seldom used pages. It is a simple algorithm to build, making it appropriate for systems with minimal resources or circumstances in which more complex algorithms are impractical. By routinely resetting reference bits, NRU can adapt to changing access patterns. Randomization is also used in page eviction to spread out the eviction load and avoid predictability.

Updated on: 17-Jul-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements