最近最久未使用算法(Last Recently Used,LRU)的字面意思即为选择最近最长时间未访问过的页面予以淘汰,它认为过去一段时间未被访问过的页面,在最近的将来可能也不会被访问。该算法为每个页面设置一个访问字段,来记录页面自上次被访问以来所经历的时间,淘汰页面时,选择现有页面中值最大的予以淘汰。
下面给出页面中最多含有5块内存大小的算法的实现代码:
public class LRU {
private int theArray[];
private int back; //定义队尾
private int currentSize; //队列中存放元素个数
private int maxSize=5; //队列中能存放元素的个数
public LRU(){
theArray=new int[maxSize];
back=0;
currentSize=0;
}
public void queue(int a[]){
for(int i = 0;i < a.length;i++){
enQueue(a[i]);
}
}
public void enQueue(int x){ //入队
beUsed(x); //先判断是否已存在该页号,若存在,删除
if(currentSize < maxSize){
theArray[back] = x;
back++;
currentSize++;
}else if(currentSize == maxSize){ //满了
for(int i = 0;i < maxSize - 1;i++){
theArray[i] = theArray[i+1];
}
theArray[maxSize-1]=x;
}
for(int i = 0;i < currentSize;i++){
System.out.print(theArray[i]);
}
System.out.println();
}
public void beUsed(int x){ //判断是否已存在该页号,若存在,删除已有的
for(int i = 0;i < currentSize;i++){
if(theArray[i] == x){
for(int j = i;j < currentSize - 1;j++){
theArray[j] = theArray[j + 1];
}
currentSize--;
back--;
}
}
}
public static void main(String[] args) {
LRU lru = new LRU();
int a[] = {4,7,0,7,1,0,1,2,1,2,6};
lru.queue(a);
}
}
分析:LRU性能较好,但需要寄存器和栈等硬件的支持,且是堆栈类算法。
实验结果如下: