These three permutation algorithms, or cache scheduling algorithms, actually originate from the operating system. The page replacement algorithm for the operating system.
FIFO: The first out algorithm.
Lru:least recently used, the least recently used algorithm. That is, the least recently used object is kicked out of memory.
Opt:optimal, the best alternative, the god mode. It takes into account the current in-memory objects, as well as the objects that will be put into memory in the future, and then kicks out the memory for the pages that are visited in the furthest future. So, when the algorithm is converted, it needs to take into account the objects that will be put into memory in the future in order to find the object to be kicked out of memory.
Take a look at the detailed examples below to understand the specific strategy of the algorithm.
Consider the following page trends:
1,2,3,4,2,1,5,6,2,1,2,3,7,6,3,2,1,2,3,6
When the number of memory blocks is 3 o'clock, ask FIFO, LRU, opt the three kinds of replacement algorithm of the number of pages?
A: The page definition is that all memory blocks are initially empty, so the pages used for the first time are generated with a single fault.
When the number of memory blocks is 3 o'clock:
The number of fault pages interrupted is 16.
In the FIFO algorithm, the page that first enters the memory is swapped out first. When page 6 to adjust, the state of the memory is 4, 1, 5, the page 6 before the page, 5, 1, 2, 4 is visible, 4 is the first to enter the memory, this time should be swapped out, and then the page 6 into memory.
The number of fault pages interrupted is 15.
In the LRU algorithm, the least recently used pages are first swapped out. When page 6 to adjust, the state of the memory is 5, 2, 1, the page 6 before the page, 5, 1, 2, see 2 for the most recent period of time the least used, this time should be swapped out, and then the page 6 into memory.
The number of fault pages interrupted is 11. In the OPT algorithm, pages that are visited in the farthest future are swapped out first. When Page 6 is to be adjusted, the state of the memory is 1, 2, 5, the page after the page 6 to be paged in 2, 1, 2, ..., 5 for the most recent period of time the least used, this time should be swapped out, and then the page 6 into memory.
via:http://yinzhezq.blog.163.com/blog/static/1648628902010112961039187/
Other than that:
There are also algorithms:
Lfu:least frequently used, the least frequently used algorithm. It is based on the number of times the page has been used to select the least used pages. Notice the difference between it and LRU.
In addition, the OPT algorithm is often used to evaluate the quality of other replacement algorithms. cannot be achieved by itself.
via:http://wenda.tianya.cn/question/7885ae7b7cc29697
Some references:
In Java, the principle and application of LRU cache scheduling algorithm based on Linkedhashmap are implemented. Search, there are a bunch of articles:
such as: http://www.cnblogs.com/-OYK/archive/2012/12/04/2801799.html
There are also many algorithms based on LRU improvements, such as:
Http://www.cnblogs.com/cyjb/archive/2012/11/16/LruCache.html
Cache Hit Ratio
On the other hand, when you're going to write a caching algorithm yourself, or to use one, or to evaluate comparisons,
What we want to focus on is the cache hit ratio problem.
If you write one yourself, it's best to simulate some data, test how big the approximate hit rate is, and then compare it to the other algorithm's hit rate.
Reprint: HTTP://CHEN.JUNCHANG.BLOG.163.COM/BLOG/STATIC/6