Page replacement algorithm LRU & LFU algorithm

Page replacement algorithm introduced

Evaluation of the quality of a page replacement algorithm has two main criteria, one hit rate is higher, the second is easier to implement algorithms. To improve the hit rate of a page replacement algorithm, first of all to make this algorithm can accurately reflect the local program, followed by the algorithm to be able to make full use of the historical information in the main memory paging situation, or in the main memory to be able to predict paging happens. 
  Page replacement algorithm is mainly used in the following places: 
  (1) virtual memory, main memory page (or block) is replaced. 
  Replacement block (2) Cache of. 
  Speed table (3) virtual memory, fast replacement table. 
  (4) The virtual storage, the replacement user base register. 

 

LFU algorithm

    Least Recently Used algorithm, that algorithm LFU (Least Frequently Used algorithm). This algorithm selects the least recently accessed page as the replaced pages. Obviously, this is a very reasonable algorithm, because the page page so far the least used, it may also be the future of the least visited. This algorithm makes full use of main memory pages scheduling of historical information, but also accurately reflect the local program. However, this algorithm is very difficult to achieve, it is set to a very long counter for each page, and to select a fixed timer counter clock for each counter. In selecting a page to be replaced, all from the counter to find the largest counter a count value. Thus, generally it follows a relatively simple method.

    The core idea: If you use a number of data in the most recent period rarely, then the possibility of being used in a future period of time is very small. "

    Traditional achieve:

    

        1. New data is added to the inserted end of the queue (because the reference count is 1);

        2. After the data in the queue is accessed, the reference count is increased, the reordering queue;

        3. When the need to eliminate data, sorted list of the last data block (minimum Visits blocks) deleted.

    Method to realize:

        A minor vertex available heap + hashmap, to achieve;

       Small top of the heap sort Visits

       hashmap to update the number of accesses to each cache node
 

 

 

      A modified version of the algorithm: (replacement within the specified time, the number of sort eliminated in accordance with the rules LFU) Window-LFU

     

  

        1) a record of the past W access to records;

        2) When the need to eliminate, access to records of W LFU eliminated in accordance with the rules of the sort

        For example as follows:

            Access history record length is assumed to 9, cache size is 3, represented by different colors for the drawing to access different data blocks, the needle repeatedly access the same color represents the same data.

            Sample 1: 3 Access yellow, blue and orange are two, orange updated, cache yellow, orange, blue three data blocks

            Sample 2: green 3 access, two blue, two dark red, blue updated, cache green, blue, dark three data blocks

         We need to maintain a queue record access historical data stream; need to sort.          

          Window-LFU only a part of the history record access, do not need to record all data access history, memory consumption and therefore sort consumption is lower than LFU.

 

LRU algorithm

     The longest not used algorithm, the LRU algorithm (Least Recently Used algorithm). This algorithm is the most recent page has not been visited for a long time as the replaced pages. It LFU algorithm "plurality" and "low" number to be recorded on the simplified judgment "Yes" and "No", therefore, relatively easy to implement. 

     The core idea: If the page is not accessed for a long time over a period of time and will not visit

    Traditional implementation principle:

        Suppose the sequence 43423142

        There are three physical block

        4 4 into memory first round

        Round 3 times into memory 34

        After 4 transferred to memory 43

        After 2 transferred to memory 243

        After 3 transferred to memory 324

        之后 1调入内存 1 3 2(因为最少使用的是4,所以丢弃4)

        之后 4调入内存 4 1 3(原理同上)

        最后 2调入内存 2 4 1

Guess you like

Origin blog.csdn.net/weixin_42831704/article/details/90764427