?? getbackusedmemory.java
字號(hào):
package com.ysu.cwy;
import java.util.List;
public class GetBackUsedMemory {
private List<MemoryBlock> memoryFreeBlockList=null; //空閑鏈
private List<MemoryBlock> memoryBlockInUseList=null; //使用鏈
private MemoryInit memoryInit=null; //內(nèi)存初始化對(duì)象
//構(gòu)造函數(shù)
public GetBackUsedMemory(List<MemoryBlock> memoryFreeBlockList,
List<MemoryBlock> memoryBlockInUseList, MemoryInit memoryInit) {
super();
this.memoryFreeBlockList = memoryFreeBlockList;
this.memoryBlockInUseList = memoryBlockInUseList;
this.memoryInit = memoryInit;
}
/**
* @function 回收內(nèi)存
* @param process 需要回收的進(jìn)程信息
*/
public synchronized void getBackUsedBlock(Process process){
long processId=process.getProcessID();
MemoryBlock needGetBackMemoryBlock=null;
MemoryBlock lastMemoryBlock=null;
MemoryBlock nextMemoryBlock=null;
for(int i=0;i<memoryBlockInUseList.size();i++){
needGetBackMemoryBlock=memoryBlockInUseList.get(i);
if(needGetBackMemoryBlock.getProcessId()==processId){
memoryBlockInUseList.remove(i);
break;
}
}
if(needGetBackMemoryBlock.getBlockStartPlace()==0){
nextMemoryBlock=memoryFreeBlockList.get(0);
if(needGetBackMemoryBlock.getBlockEndPlace()==(nextMemoryBlock.getBlockStartPlace()-1)){
nextMemoryBlock.setBlockStartPlace(0);
nextMemoryBlock.setBlockSize(nextMemoryBlock.getBlockSize()+needGetBackMemoryBlock.getBlockSize());
memoryFreeBlockList.remove(0);
memoryFreeBlockList.add(0, nextMemoryBlock);
return;
}else{
needGetBackMemoryBlock.setBlockStatus(false);
needGetBackMemoryBlock.setProcessId(-1);
needGetBackMemoryBlock.setFullBlock(false);
memoryFreeBlockList.add(0, needGetBackMemoryBlock);
return;
}
}else if(needGetBackMemoryBlock.getBlockEndPlace()==memoryInit.getMaxMemoryAddress()){
int memoryFreeBlockListSize=memoryFreeBlockList.size()-1;
lastMemoryBlock=memoryFreeBlockList.get(memoryFreeBlockListSize);
if((needGetBackMemoryBlock.getBlockStartPlace()-1)==lastMemoryBlock.getBlockEndPlace()){
lastMemoryBlock.setBlockEndPlace(memoryInit.getMaxMemoryAddress());
lastMemoryBlock.setBlockSize(lastMemoryBlock.getBlockSize()+needGetBackMemoryBlock.getBlockSize());
memoryFreeBlockList.remove(memoryFreeBlockListSize);
memoryFreeBlockList.add(memoryFreeBlockListSize, lastMemoryBlock);
return;
}else{
needGetBackMemoryBlock.setBlockStatus(false);
needGetBackMemoryBlock.setProcessId(-1);
needGetBackMemoryBlock.setFullBlock(false);
memoryFreeBlockList.add(needGetBackMemoryBlock);
return;
}
}else{
int p=0;
int memoryBlockSize=0;
nextMemoryBlock=memoryFreeBlockList.get(p);
while(needGetBackMemoryBlock.getBlockStartPlace()>nextMemoryBlock.getBlockEndPlace()){
p++;
nextMemoryBlock=memoryFreeBlockList.get(p);
}
lastMemoryBlock=memoryFreeBlockList.get(p-1);
if(((needGetBackMemoryBlock.getBlockStartPlace()-1)==lastMemoryBlock.getBlockEndPlace())
&&(needGetBackMemoryBlock.getBlockEndPlace()==(nextMemoryBlock.getBlockStartPlace()-1))){
if(lastMemoryBlock.getBlockSize()>=nextMemoryBlock.getBlockSize()){
memoryBlockSize=nextMemoryBlock.getBlockSize();
nextMemoryBlock.setBlockStartPlace(needGetBackMemoryBlock.getBlockStartPlace());
nextMemoryBlock.setBlockSize(memoryBlockSize+needGetBackMemoryBlock.getBlockSize());
memoryFreeBlockList.remove(p);
memoryFreeBlockList.add(p, nextMemoryBlock);
return ;
}else{
memoryBlockSize=lastMemoryBlock.getBlockSize();
lastMemoryBlock.setBlockEndPlace(needGetBackMemoryBlock.getBlockEndPlace());
lastMemoryBlock.setBlockSize(memoryBlockSize+needGetBackMemoryBlock.getBlockSize());
memoryFreeBlockList.remove(p-1);
memoryFreeBlockList.add(p-1, lastMemoryBlock);
}
}else if(((needGetBackMemoryBlock.getBlockStartPlace()-1)!=lastMemoryBlock.getBlockEndPlace())
&&(needGetBackMemoryBlock.getBlockEndPlace()!=(nextMemoryBlock.getBlockStartPlace()-1))){
needGetBackMemoryBlock.setFullBlock(false);
needGetBackMemoryBlock.setProcessId(-1);
needGetBackMemoryBlock.setBlockStatus(false);
memoryFreeBlockList.add(p, needGetBackMemoryBlock);
return ;
}else if(((needGetBackMemoryBlock.getBlockStartPlace()-1)==lastMemoryBlock.getBlockEndPlace())
&&(needGetBackMemoryBlock.getBlockEndPlace()!=(nextMemoryBlock.getBlockStartPlace()-1))){
memoryBlockSize=lastMemoryBlock.getBlockSize();
lastMemoryBlock.setBlockEndPlace(needGetBackMemoryBlock.getBlockEndPlace());
lastMemoryBlock.setBlockSize(needGetBackMemoryBlock.getBlockSize()+memoryBlockSize);
memoryFreeBlockList.remove(p-1);
memoryFreeBlockList.add(p-1, lastMemoryBlock);
return ;
}else{
memoryBlockSize=nextMemoryBlock.getBlockSize();
nextMemoryBlock.setBlockStartPlace(needGetBackMemoryBlock.getBlockStartPlace());
nextMemoryBlock.setBlockSize(memoryBlockSize+needGetBackMemoryBlock.getBlockSize());
memoryFreeBlockList.remove(p);
memoryFreeBlockList.add(p, nextMemoryBlock);
return ;
}
}
}//end method getBackUsedBlock
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -