First, the LRU algorithm introduces
The LRU algorithm is the least recently unused algorithm. When the memory page is missing, it is always preferable to select the pages that are unused from the current longest and swap out the current one. The algorithm can be implemented by stack simulation.
The top of the stack always saves the currently visited page number, and the bottom of the stack always holds the page number that has not been accessed for the longest time. for the next page, there are two things:
① hit, you need to: Update the top element of the stack. Puts the current hit page number to the top of the stack.
② misses, there is also a need to consider whether the stack is full. 1) If the stack is not full, directly put the missing page number to the top of the stack. 2) The stack is full, you need to select a page to swap out (the bottom element is the longest page not visited), and then put the new page on the top of the stack.
Second, the Code implementation
Importjava.util.LinkedList; Public classLRU {PrivateLinkedlist<integer> Stack;//analog page ' register ' Private intSize//register size, indicating how many pages can be loaded altogether PublicLRU (intsize) {Stack=NewLinkedlist<>(); This. Size =size; } //LRU algorithm Simple implementation, return total number of misses Public intLruint[] pagenumbers) { if(Size <= 0 | | pagenumbers = =NULL) Throw NewIllegalArgumentException ("Illegal arugments"); if(Pagenumbers.length <=size)returnpagenumbers.length; intUnhit = 0; for(inti = 0; i < pagenumbers.length; i++) { intindex =Ishit (Pagenumbers[i]); if(Index = =-1) Unhit=Processunhit (Pagenumbers[i], unhit); Else{processhit (pagenumbers[i], index); } } returnUnhit; } /** * * @parampagenumber Judge if PageNumber hit *@return-1 means unhit, others indicate hit*/ Private intIshit (intpagenumber) { returnStack.indexof (pagenumber); } /*** When the stack is not full, the missing page number directly into the stack, when the stack full, you need to replace the page, first select a page (bottom) Delete, and then push the new page *@parampagenumber Missing page number *@paramCount Current Misses *@returnnumber of missed hits after update*/ Private intProcessunhit (intPageNumber,intcount) { if(Isfull ()) stack.removelast ();//Delete pages that have not been visited for the longest timeStack.push (pagenumber);//put on recently visited pagescount++;//number of misses plus 1 returncount; } //handling a hit situation Private voidProcesshit (intPageNumber,intindex) {Stack.push (Stack.remove (index)); } //determine if the ' register ' stack is full Private BooleanIsfull () {if(Stack.size () <size)return false; Else return true; } //Test Public Static voidMain (string[] args) {int[] PageNumbers = {4,7,1,1,7,2,1}; intSize = 2; LRU LRU=NewLRU (size); System.out.println (LRU.LRU (pagenumbers)); }}
Third, the complexity of the analysis
Because the Java.util.LinkedList realizes the function of the stack. The push () method always places elements into the header , and the Pop () method always removes the elements from the table header of the linked list. Here, the table header of the list represents the top of the stack.
Therefore, when a page number hits, you need to find the page's position (index) from the list, then delete the page and push it to the table header of the linked list. ---processhit () method
Because it is a linked list, the time complexity of looking for a page is O (N), the worst case is to scan the entire list.
When the page misses, you need to push the page number to the list header. Before push, check that the stack is full. If not full, push directly into the stack, the time complexity is O (1), if the stack is full, you need to delete the list footer element (equivalent to the stack bottom element-removelast () time complexity is also O (1), Because LinkedList is essentially a doubly linked list. then push the deleted element to the top of the stack--time complexity O (1)
The total time complexity of processing is still O (1) when missing
--LRU Algorithm for memory page substitution algorithm using java.util.LinkedList simulation