Virtual paged storage management service
Collection
zero Useful+1
zero
synonym Least recently used (Computer science and technology terms) generally refers to LRU
LRU is the abbreviation of Least Recently Used Page replacement algorithm , select the most recently unused page to eliminate. The algorithm assigns each page An access field is used to record the time t of a page since it was last accessed. When a page needs to be eliminated, select the page with the largest t value among the existing pages, that is, the least recently used page.
Chinese name
Least recently used
Foreign name
Least Recently Used
Discipline
computer
Abbreviation
LRU
Relevant terms
Page replacement algorithm
Application
memory management

brief introduction

Announce
edit
Figure 1 LRU page replacement algorithm
Most of the least recently used algorithms (LRU) operating system A page replacement algorithm widely used to maximize the page hit rate. The idea of this algorithm is to select the page that has not been used for the longest time to replace when page fault interruption occurs. [1] From the principle of program operation, the least recently used algorithm is close to the ideal one Page replacement algorithm This algorithm not only makes full use of the historical information of page calls in memory, but also correctly reflects the local problems of the program. The result of page replacement using LRU algorithm for the above example is shown in Figure 1. When the process accesses Page 2 for the first time, Page 7 is replaced because it has not been visited for the longest time. When the process accesses Page 3 for the first time, Page 1 becomes the most recently unused page and is replaced. It can be seen from Figure 1 that the images of the first five times are the same as those of the best replacement algorithm, but this is not an inevitable result. Because the best replacement algorithm is based on the "backward looking" point of view, that is, it is based on the use of subsequent pages; The LRU algorithm is "forward looking", that is, it is judged according to the previous use of each page, but there is no inevitable link between the past and future directions of the page.

Hardware support

Announce
edit
Although LRU replacement algorithm is a better algorithm, it requires the system to have more supporting hardware. In order to understand how long each page of a process in memory has not been accessed by the process, and how to quickly know which page is the most recently unused page, one of two types of hardware must be supported: registers or stacks.
register
In order to record the usage of each page of a process in memory, a shift register , can be expressed as
R = R n-1 R n-2 R n-3 … R two R one R zero
Figure 2 LRU access of a process with 8 pages
When a process accesses a physical block, the corresponding register R of n -1 Position is 1. At this time, the timing signal will shift the register to the right one bit every certain time (for example, 100 ms). If we regard the number of n-bit registers as an integer, then the page corresponding to the register with the smallest value is the most recently unused page. Figure 2 shows the LRU access when a process has 8 pages in memory and an 8-bit register is configured for each memory page. Here, the serial numbers of eight memory pages are set as 1-8. As can be seen from the figure, the R value of the third memory page is the smallest. When a page fault occurs, replace it first.
Stack
Figure 3 Changes of the stack when saving the currently used page with the stack
A special stack can be used to save the page number of each currently used page. Whenever a process accesses a page, it moves the page number of the page from the stack and pushes it to the top of the stack. Therefore, the top of the stack is always the number of the most recently accessed page, while the bottom of the stack is the number of the most recently unused page. Assume that the page number sequence of the page visited by an existing process is:
4,7,0,7,1,0,1,2,1,2,6
As the process accesses, the page number in the stack changes as shown in Figure 3. A page fault occurs when accessing page 6. At this time, page 4 is the most recently unreachable page, and it should be replaced. [2]

Other page replacement algorithms

Announce
edit
stay process During the running process, if the pages to be accessed are not in the memory and need to be transferred into the memory, but the memory has no free space, in order to ensure the normal operation of the process, the system must call a page of programs or data from the memory to the swap area of the disk. But which page should be called out must be determined according to a certain algorithm. Usually, the algorithm for selecting page swapping out is called Page replacement algorithm (Page-Replacement Algorithms)。 The quality of the replacement algorithm will directly affect the performance of the system. A good page replacement algorithm should have a lower page replacement frequency. In theory, the pages that will not be visited in the future should be replaced, or the pages that will not be visited in a long time should be replaced. There are many permutation algorithms, which try to be closer to the theoretical goal.
Optimal permutation algorithm (OPT)
This is an ideal page replacement algorithm, but it is actually impossible to achieve. The basic idea of the algorithm is that when a page fault occurs, some pages are in memory, and one of them will be accessed soon (including the next instructions Other pages may not be accessed until 10, 100, or 1000 instructions. Each page can be marked with the number of instructions to be executed before the page is accessed for the first time. Optimal page replacement algorithm It simply stipulates that the page with the largest tag should be replaced. The only problem with this algorithm is that it cannot be implemented. When page faults occur, the operating system cannot know when each page will be accessed next. Although this algorithm is impossible to implement, the best page replacement algorithm can be used to measure and compare the performance of realizable algorithms.
First in first out replacement algorithm (FIFO)
The simplest Page replacement algorithm It is the first in, first out (FIFO) method. The essence of this algorithm is to always select the page with the longest (i.e. the oldest) stay in main memory for replacement, that is, the page that enters memory first and exits memory first. The reason is that the page that was first called into memory is more likely to be no longer used than the page that was just called into memory. Establish a FIFO queue to hold all pages in memory. The replaced page is always on the queue header. When a page is put into memory, it is inserted at the end of the queue. This algorithm is ideal only when the address space is accessed in a linear order, otherwise the efficiency is not high. Because those pages that are often visited often stay in main memory for the longest time, they have to be replaced because they become "old".
Another disadvantage of FIFO is that it has an abnormal phenomenon, that is, when adding storage blocks, the page fault interrupt rate increases. Of course, the page trend that causes this abnormal phenomenon is actually rare.
Least use (LFU) replacement algorithm
When using the least use replacement algorithm, one should be set for each page in memory shift register , used to record the frequency of the page being visited. The replacement algorithm selects the pages that are used least in the previous period as the elimination pages. Because the memory has a high access speed, such as 100 ns, it may access a page thousands of times in a continuous time of 1 ms. Therefore, it is usually not possible to directly use the counter to record the number of times a page is accessed, but to use the shift register method. Each time a page is accessed, the highest position 1 of the shift register is shifted to the right once every certain time (for example, 100 ns). In this way, the page with the least use in the recent period will be the page with the smallest ∑ Ri. The page access graph of the LFU replacement algorithm is identical to that of the LRU replacement algorithm; In other words, using such a set of hardware can realize both LRU algorithm and LFU algorithm. It should be pointed out that the LFU algorithm does not really reflect the usage of the page, because in each time interval, only one bit of the register is used to record the usage of the page. Therefore, one visit and 10000 visits are equivalent. [3]