操作系统实验 内存管理文档格式.docx
《操作系统实验 内存管理文档格式.docx》由会员分享,可在线阅读,更多相关《操作系统实验 内存管理文档格式.docx(14页珍藏版)》请在冰点文库上搜索。
主要代码:
publicclassProcess{
privateStringname;
privateintsize;
privateintbeginPx;
privateintendPx;
publicintgetBeginPx(){
returnbeginPx;
}
publicvoidsetBeginPx(intbeginPx){
this.beginPx=beginPx;
publicintgetEndPx(){
returnendPx;
publicvoidsetEndPx(intendPx){
this.endPx=endPx;
publicStringgetName(){
returnname;
publicvoidsetName(Stringname){
this.name=name;
publicintgetSize(){
returnsize;
publicvoidsetSize(intsize){
this.size=size;
}
2.根据用户输入而分配内存的大小,若输入的大小大于目前可分配内存的大小则拒绝分配操作,否则增加一个新进程入链表中,并在已分配表中增加进程的名字,更新剩余内存大小。
分配内存流程图:
绘画paint()方法的主要代码:
publicvoidpaint(Graphicsg){
super.paint(g);
//刷新页面
Colorc=g.getColor();
g.setColor(Color.WHITE);
g.fillRect(20,400,450,50);
//若已有分配内存
if(myList.size()!
=0){
g.setColor(Color.BLACK);
//从已分配的内存中循环获取像素起点位置和像素的大小
for(inti=0;
i<
myList.size();
i++){
g.fillRect(myList.get(i).getBeginPx(),400,myList.get(i).getEndPx()-myList.get(i).getBeginPx(),50);
g.setColor(c);
}
}
显示已分配进程名字的showListName()方法主要代码:
publicvoidshowListName(ArrayList<
Process>
myList){
ProcessnewProcess=null;
showList.removeAll();
for(inti=0;
newProcess=myList.get(i);
showList.add(newProcess.getName());
}
分配内存的监视器主要代码:
publicclassAssignMemoryListenerimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
//当输入分配内存大小不完整时提示
if((mbField.getText().equals("
"
))||(kbField.getText().equals("
))||(byteField.getText().equals("
))){
JOptionPane.showMessageDialog(null,"
请完整输入所需分配内存的大小"
"
警告对话框"
JOptionPane.WARNING_MESSAGE);
else{
//当内存不足时
if(Integer.parseInt(mbField.getText())*1024*1024+Integer.parseInt(kbField.getText())*1024+Integer.parseInt(byteField.getText())>
(m*1024*1024+k*1024+b)){
JOptionPane.showMessageDialog(null,"
所剩内存不足"
else{
Stringname=JOptionPane.showInputDialog(null,"
输入进程名字"
输入对话框"
JOptionPane.PLAIN_MESSAGE);
//获得分配内存名字
ProcessnewProcess=newProcess();
newProcess.setName(name);
newProcess.setSize(Integer.parseInt(mbField.getText())*1024*1024+Integer.parseInt(kbField.getText())*1024+Integer.parseInt(byteField.getText()));
size=newProcess.getSize();
fsize=size;
//把size转成float类型计算,小数计算必须要化成整形
px=(int)(450/p*fsize);
//获得像素的长度
newProcess.setBeginPx(index);
newProcess.setEndPx(index+px);
index=index+px;
myList.add(newProcess);
//把进程添加到链表中
//绘图
repaint();
showListName(myList);
//显示所用进程的名字
//三个输入框清空
mbField.setText("
);
kbField.setText("
byteField.setText("
//更改剩余内存值
intleftSize=(m*1024*1024+k*1024+b)-size;
m=leftSize/(1024*1024);
k=leftSize/1024%1024;
b=leftSize%1024;
notice2.setText("
可分配最大内存:
+m+"
MB"
+k+"
KB"
+b+"
Byte"
输入要分配的内存大小:
图1分配内存
输入名字:
图2输入名字
分配两个内存:
图3分配两个内存
3.用户选择已分配的内存名字而回收相应的内存模块:
回收流程图:
回收内存主要代码:
publicclassCollectMemoryListenerimplementsActionListener{
publicvoidactionPerformed(ActionEventarg0){
intnum=showList.getSelectedIndex();
//获得被选中的序号
//更新剩余内存值
intreleaseSize=myList.get(num).getSize();
m=m+releaseSize/(1024*1024);
k=k+releaseSize/1024%1024;
b=b+releaseSize%1024;
notice2.setText("
//从链表中删去该进程
myList.remove(num);
showListName(myList);
repaint();
回收第一个内存后,绘图更新,剩余内存更新:
图4回收第一个已分配的内存
重新整合内存主要代码:
publicclassRefreshMemoryListenerimplementsActionListener{
index=20;
size=myList.get(i).getSize();
//把size转成float类型计算
myList.get(i).setBeginPx(index);
myList.get(i).setEndPx(index+px);
repaint();
图5重新整合内存
四、实验总结
通过本次实验,加深了对内存管理的理解。
并掌握了单链表应用的重要方法。
运用java的GUI为客户端整体布局,并且运用paint()方法来表示内存分配的状态。
一个已分配的内存表示一个对象,所有对象都存放在链表中,当回收内存时则把改对象在链表中移除。
每次绘图都是根据链表中存在的对象而绘画。
由于对象中存放有起点的像素和终点的像素,因而实现起来简单。