The page replacement algorithm for memory management

Source: Internet
Author: User

In the process of address mapping, a page fault is generated if the page that you want to access is no longer in memory. When a page break occurs, the operating system must select one of the pages in memory to move it out of memory to make room for the page that is about to be paged in. The rule used to choose which page to retire is called the page replacement algorithm. The common permutation algorithms are:

1) Best replacement algorithm (OPT) (Ideal replacement algorithm)

This is an ideal page replacement algorithm, but it is virtually impossible to implement. The basic idea of this algorithm is that when a page fault occurs, some pages are in memory, and one of them will be accessed very quickly (also containing the page of the next instruction immediately followed), while the other pages may be accessed after 10, 100, or 1000 instructions. Each page can be tagged with the number of instructions to be executed before the page is first accessed. The best page replacement algorithm simply stipulates that the most marked pages should be replaced. The only problem with this algorithm is that it cannot be implemented. When a page fault occurs, the operating system cannot know when the next time the individual pages are accessed. Although this algorithm is not possible, the best page replacement algorithm can be used to measure the performance of an achievable algorithm.

2) Advanced first-out permutation algorithm (FIFO)

The simplest page substitution algorithm is the first in, first Out (FIFO) method. The essence of this algorithm is to always choose to stay in main memory for the longest (that is, the oldest) of a page permutation, that is, the first page to enter the RAM, first exit memory . The reason for this is that the first page to be paged into memory is more likely to be used than it was when it was just transferred into memory. Create a FIFO queue that hosts all the pages in memory. The displaced 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 it is inefficient. Because those pages are often visited, often in main memory also stay the longest, the result they become "old" and have to be replaced.
Another drawback of FIFO is that it has an anomaly, that is, increasing the memory block, instead of increasing the fault rate of the pages. Of course, the page trend that causes this anomaly is actually very rare.

3) The most recent unused (LRU) algorithm

The main difference between the FIFO algorithm and the OPT algorithm is that the FIFO algorithm uses the time of the page to enter memory as the replacement basis, and the OPT algorithm is based on the future use of the page time. If the recent past is an approximation of the near future, you can replace pages that have not been used for the longest time in the past. The essence of this is that when you need to replace a page,Choose a page that has been unused for the longest time in recent times。 This algorithm is calledthe longest unused algorithm(Least recently USED,LRU).
The LRU algorithm is related to the last time each page was used. When a page has to be replaced, the LRU algorithm chooses which pages have been unused for the longest time in the past.
The LRU algorithm is a frequently used page replacement algorithm and is considered quite good, but there are problems in how to implement it. The LRU algorithm requires real hardware support. The question is how to determine the order of the last use time, there are two possible ways to do this:
1. Counter. The simplest case is to have each page table item correspond to a usage time field and add a logical clock or counter to the CPU. Each storage access, the clock is added 1. Whenever a page is accessed, the contents of the clock register are copied to the usage time field of the corresponding page table entry. This way we can always keep the "time" that each page accesses last. When you displace a page, select the page with the lowest time value. To do this, not only to look up the page table, and when the page table changes (due to CPU scheduling) to maintain the time in the page table, but also to consider the problem of clock value overflow.
2. Stack. Keep the page number with a stack. Whenever a page is accessed, it is removed from the stack and placed on top of the stack. This way, the top of the stack always has the most used pages, and the bottom of the stack is the least used page. Because you want to remove an item from the middle of the stack, you should connect it with a bidirectional chain with a tail-pointing pointer. In the worst case, moving a page and placing it on top of the stack requires a change of 6 pointers. Each modification has a cost, but the need to replace which page can be directly obtained, not to find, because the tail pointer to the bottom of the stack, where there are displaced pages.
The implementation of the LRU algorithm requires a lot of hardware support, but also a certain amount of software overhead. So the actual implementation is a simple and effective LRU approximation algorithm.
An LRU approximation algorithm isalgorithms not recently used(not recently Used,nur). It adds a reference bit to each table item that stores the Chunked table, and the operating system periodically resets them to 0. When a page is accessed, the location is 1 by the hardware. After a while, check these bits to determine which pages were used and which pages have not been used since the last 0. You can eliminate the 0 page because it has not been visited in the most recent time.

4) Clock replacement algorithm (the approximate implementation of LRU algorithm)

5) Minimum Use (LFU) permutation algorithm

When using the least-used permutation algorithm, you should set up a shift register for each page in memory to record how often the page is accessed. The permutation algorithm chooses the least-used page in recent times as the elimination page. Because the memory has a high access speed, such as 1 NS, it is possible to access tens of thousands of times in a single page for a period of time, so it is often impossible to use the counter directly to record the number of times a page is accessed, instead of using a shift register. Each time a page is accessed, the top position of the shift register is 1, and then the right shift occurs at a certain time (for example, in NS). This way, the least-used page in the most recent time will be the ∑ri smallest page.

The page access diagram of the LFU permutation algorithm is identical to the access graph of the LRU permutation algorithm, or the use of such a hardware can realize the LRU algorithm and the LFU algorithm. It should be pointed out that the LFU algorithm does not really reflect the use of the page, because at each time interval, just use one of the registers to record the use of the page, so access once and access 10 000 times is equivalent.

Reference: http://hary.wang.blog.163.com/blog/static/11695172820104945040438/

6) Working Set algorithm

Reference: http://hi.baidu.com/binbin_88115/blog/item/f2787a386d2eb7d8d5622595.html

7) Working Set clock algorithm

8) Aging algorithm (very similar to LRU's efficient algorithm)

9) NRU (not recently used) algorithm

10) Second Chance algorithm

The basic idea of the second chance algorithm is the same as the FIFO, but it has been improved to avoid replacing the frequently used pages. When you select the Displacement page, check its access bits. If it is 0, the page will be eliminated; If the access bit is 1, give it a second chance and select the next FIFO page. When a page gets a second chance, its access bit is cleared to 0, and its arrival time is set to the current time. If the page was accessed during this period, access location 1. This gives the second chance the page will not be eliminated, until all other pages have been eliminated (or given a second chance). Therefore, if a page is used frequently, its access bit will always remain at 1, and it will never be eliminated.
The second chance algorithm can be considered as a ring queue. Use a pointer to indicate which page is to be eliminated below. When a storage block is needed, the pointer advances until it finds a page with a 0 access bit. As the pointer progresses, the access bit is cleared to 0. In the worst case, all access bits are 1, the pointer passes through the entire queue for a week, and each page gives a second chance. This will degenerate into a FIFO algorithm.

The page replacement algorithm for memory management

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.