📄 getbackusedmemory.java
字号:
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; //内存初始化对象
//构造函数
public GetBackUsedMemory(List<MemoryBlock> memoryFreeBlockList,
List<MemoryBlock> memoryBlockInUseList, MemoryInit memoryInit) {
super();
this.memoryFreeBlockList = memoryFreeBlockList;
this.memoryBlockInUseList = memoryBlockInUseList;
this.memoryInit = memoryInit;
}
/**
* @function 回收内存
* @param process 需要回收的进程信息
*/
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
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -