内存的分配与回收
一、 实验目的
用高级语言完成一个主存空间的分配和回收程序,以加深对动态分区分配方式及其算法的理解。
二、 实验内容和要求
采用连续分配方式之动态分区分配存储管理,使用首次适应算法、循环首次适应算法、最佳适应算法和最坏适应算法4种算法完成设计(任选两种算法)。
(1)**设计一个作业申请队列以及作业完成后的释放顺序,实现主存的分配和回收。采用分区说明表进行。
(2)或在程序运行过程,由用户指定申请与释放。
(3)设计一个空闲区说明表,以保存某时刻主存空间占用情况。把空闲区说明表的变化情况以及各作业的申请、释放情况显示。
三、 实验方法、步骤及结果测试
- 1. 原理分析及流程图
程序名:best.rar
- 2. 步骤:
创建项目:best
创建包:best
创建类:bestFit.java 、firstFit.java、 MainFrame.java
编写代码
- 3. 主要程序段及其解释
package best; import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.Font; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.border.EtchedBorder; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JLabel; import java.awt.Color; import javax.swing.SwingConstants; import javax.swing.JTextField; public class bestFit extends JFrame { private JPanel contentPane; private JTextField everySize; private JTextField workname; private JTextField count; private JFrame frame; private JTextField location; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { bestFit frame = new bestFit(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ class pointer { public int prepointer; public int nextpoint; public int length; public int signal; pointer() { prepointer=0; nextpoint=0; length=0; signal=0; } } public bestFit() { setTitle(""); setResizable(false); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setBounds(300, 200, 559, 447); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JPanel panel = new JPanel(); panel.setLayout(null); panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, Color.DARK_GRAY, Color.PINK)); panel.setBounds(10, 10, 324, 399); contentPane.add(panel); JLabel label = new JLabel("模拟内存"); label.setHorizontalAlignment(SwingConstants.CENTER); label.setForeground(Color.GRAY); label.setFont(new Font("微软雅黑", Font.BOLD, 18)); label.setBounds(64, 0, 118, 36); panel.add(label); JPanel panel_1 = new JPanel(); panel_1.setLayout(null); panel_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, Color.BLACK, Color.DARK_GRAY)); panel_1.setBounds(99, 50, 176, 312); panel.add(panel_1); JPanel one = new JPanel(); one.setLayout(null); one.setBorder(new EtchedBorder(EtchedBorder.RAISED, Color.BLACK, null)); one.setBounds(0, 0, 176, 54); panel_1.add(one); JPanel two = new JPanel(); two.setLayout(null); two.setBorder(new EtchedBorder(EtchedBorder.RAISED, new Color(0, 0, 0), null)); two.setBounds(0, 54, 176, 104); panel_1.add(two); JPanel three = new JPanel(); three.setLayout(null); three.setBorder(new EtchedBorder(EtchedBorder.RAISED, Color.BLACK, null)); three.setBounds(0, 158, 176, 154); panel_1.add(three); JLabel label_1 = new JLabel("50KB"); label_1.setHorizontalAlignment(SwingConstants.CENTER); label_1.setFont(new Font("微软雅黑", Font.BOLD, 14)); label_1.setBounds(270, 68, 54, 19); panel.add(label_1); JLabel label_2 = new JLabel("100KB"); label_2.setHorizontalAlignment(SwingConstants.CENTER); label_2.setFont(new Font("微软雅黑", Font.BOLD, 14)); label_2.setBounds(270, 144, 54, 19); panel.add(label_2); JLabel label_3 = new JLabel("150KB"); label_3.setHorizontalAlignment(SwingConstants.CENTER); label_3.setFont(new Font("微软雅黑", Font.BOLD, 14)); label_3.setBounds(270, 292, 54, 19); panel.add(label_3); JPanel panel_5 = new JPanel(); panel_5.setLayout(null); panel_5.setBorder(new EtchedBorder(EtchedBorder.LOWERED, Color.DARK_GRAY, Color.PINK)); panel_5.setBounds(344, 10, 199, 399); contentPane.add(panel_5); JButton back = new JButton("回收进程"); back.setFont(new Font("微软雅黑", Font.BOLD, 15)); back.setBounds(49, 316, 132, 34); panel_5.add(back); JLabel label_4 = new JLabel("进程大小"); label_4.setOpaque(true); label_4.setHorizontalAlignment(SwingConstants.CENTER); label_4.setForeground(Color.GRAY); label_4.setFont(new Font("微软雅黑", Font.BOLD, 15)); label_4.setBounds(15, 119, 65, 27); panel_5.add(label_4); everySize = new JTextField(); everySize.setHorizontalAlignment(SwingConstants.CENTER); everySize.setFont(new Font("微软雅黑", Font.BOLD, 15)); everySize.setColumns(10); everySize.setBounds(91, 121, 65, 21); panel_5.add(everySize); JLabel label_5 = new JLabel("进程名"); label_5.setOpaque(true); label_5.setHorizontalAlignment(SwingConstants.CENTER); label_5.setForeground(Color.GRAY); label_5.setFont(new Font("微软雅黑", Font.BOLD, 15)); label_5.setBounds(15, 64, 65, 27); panel_5.add(label_5); workname = new JTextField(); workname.setHorizontalAlignment(SwingConstants.CENTER); workname.setFont(new Font("微软雅黑", Font.BOLD, 15)); workname.setColumns(10); workname.setBounds(91, 66, 65, 21); panel_5.add(workname); JLabel label_6 = new JLabel("KB"); label_6.setHorizontalAlignment(SwingConstants.CENTER); label_6.setForeground(Color.GRAY); label_6.setFont(new Font("微软雅黑", Font.BOLD, 15)); label_6.setBounds(155, 122, 26, 21); panel_5.add(label_6); JButton startbutton = new JButton("开始分配"); startbutton.setFont(new Font("微软雅黑", Font.BOLD, 15)); startbutton.setBounds(49, 178, 132, 34); panel_5.add(startbutton); JButton restartbutton = new JButton("重新开始"); restartbutton.setFont(new Font("微软雅黑", Font.BOLD, 15)); restartbutton.setBounds(49, 245, 132, 34); panel_5.add(restartbutton); count = new JTextField(); count.setText("0"); count.setEnabled(false); count.setEditable(false); count.setColumns(10); count.setBounds(72, 253, 19, 15); panel_5.add(count); count.setVisible(false); location = new JTextField(); location.setEditable(false); location.setEnabled(false); location.setText("0"); location.setBounds(72, 324, 66, 21); panel_5.add(location); location.setColumns(10); location.setVisible(false); final JLabel name[]=new JLabel[20]; for(int i=0;i<20;i++) { name[i]=new JLabel(); name[i].setFont(new Font("微软雅黑",Font.BOLD,12)); name[i].setHorizontalAlignment(SwingConstants.CENTER); panel.add(name[i]); } final JLabel blocks[]=new JLabel[300]; for(int i=0;i<50;i++) { blocks[i] = new JLabel(""); blocks[i].setOpaque(false); one.add(blocks[i]); } for(int i=50;i<150;i++) { blocks[i] = new JLabel(""); blocks[i].setOpaque(false); two.add(blocks[i]); } for(int i=150;i<300;i++) { blocks[i] = new JLabel(""); blocks[i].setOpaque(false); three.add(blocks[i]); } //设置前指针和后指针,以及空闲区的初始长度 final pointer pt[]=new pointer[3]; for(int i=0;i<3;i++) { pt[i]=new pointer(); } pt[0].prepointer=0; pt[0].nextpoint=50; pt[0].length=50; pt[0].signal=0; pt[1].prepointer=50; pt[1].nextpoint=150; pt[1].length=100; pt[1].signal=0; pt[2].prepointer=150; pt[2].nextpoint=300; pt[2].length=150; pt[2].signal=0; startbutton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String str=everySize.getText(); int size=Integer.parseInt(str); String str1=count.getText(); int num=Integer.parseInt(str1); String str2=workname.getText(); int i; for(i=0;i<3;i++) { if(pt[i].length>size) { break; } } if(i==3) { JOptionPane.showMessageDialog(frame, "没有合适的空闲分区!", null, JOptionPane.WARNING_MESSAGE); } else { int sig=min(pt[0].length,pt[1].length,pt[2].length,size); for(int j=0;j<size;j++) { if(pt[sig].prepointer<50) { blocks[j+pt[sig].prepointer].setBounds(2, j+2+pt[sig].prepointer, 172, 1); blocks[j+pt[sig].prepointer].setBackground(new Color(250,0,0)); blocks[j+pt[sig].prepointer].setOpaque(true); } if(pt[sig].prepointer<150&&pt[sig].prepointer>=50) { blocks[j+pt[sig].prepointer].setBounds(2, j+2+pt[sig].prepointer-50, 172, 1); blocks[j+pt[sig].prepointer].setBackground(new Color(0,250,0)); blocks[j+pt[sig].prepointer].setOpaque(true); } if(pt[sig].prepointer<300&&pt[sig].prepointer>=150) { blocks[j+pt[sig].prepointer].setBounds(2, j+2+pt[sig].prepointer-150, 172, 1); blocks[j+pt[sig].prepointer].setBackground(new Color(0,0,250)); blocks[j+pt[sig].prepointer].setOpaque(true); } } pt[sig].prepointer=pt[sig].prepointer+size; pt[sig].length=pt[sig].length-size; name[num].setText(str2+" "+size+"KB"); name[num].setBounds(10, pt[sig].prepointer+50-size, 79, 15); num++; count.setText(num+""); } } }); restartbutton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { pt[0].prepointer=0; pt[0].nextpoint=50; pt[0].length=50; pt[0].signal=0; pt[1].prepointer=50; pt[1].nextpoint=150; pt[1].length=100; pt[1].signal=0; pt[2].prepointer=150; pt[2].nextpoint=300; pt[2].length=150; pt[2].signal=0; for(int i=0;i<300;i++) { blocks[i].setOpaque(false); blocks[i].setBackground(new Color(240,240,240)); } everySize.setText(""); for(int i=0;i<20;i++) { name[i].setText(""); } workname.setText(""); } }); back.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String str2=workname.getText(); System.out.println(str2); if(str2.equals("1")==true){ System.out.println("haha"); pt[0].prepointer=0; pt[0].nextpoint=50; pt[0].length=50; pt[0].signal=0; for(int i=0;i<50;i++) { blocks[i].setOpaque(false); blocks[i].setBackground(new Color(240,240,240)); } name[0].setText(""); } if(str2.equals("3")==true){ System.out.println("haha"); pt[2].prepointer=150; pt[2].nextpoint=300; pt[2].length=150; pt[2].signal=0; for(int i=150;i<300;i++) { blocks[i].setOpaque(false); blocks[i].setBackground(new Color(240,240,240)); } name[2].setText(""); } if(str2.equals("2")==true){ System.out.println("haha"); pt[1].prepointer=50; pt[1].nextpoint=150; pt[1].length=100; pt[1].signal=0; for(int i=50;i<150;i++) { blocks[i].setOpaque(false); blocks[i].setBackground(new Color(240,240,240)); } name[1].setText(""); } } }); } int min(int a,int b,int c,int d) { int n1=a-d; int n2=b-d; int n3=c-d; if(n1<0) { if(n2<0) { return 2; } else { if(n3<0) { return 3; } else { if(n2<=n3) { return 1; } else { return 2; } } } } else { if(n2<0) { if(n3<0) { return 0; } else { if(n1<=n3) { return 0; } else { return 2; } } } else { if(n3<0) { if(n1<=n2) { return 0; } else { return 1; } } else { if(n1<=n2) { if(n1<=n3) { return 0; } else { return 2; } } else { if(n2<=n3) { return 1; } else { return 0; } } } } } } } //首次适应算法 package best; import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JButton; import java.awt.Font; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JLabel; import javax.swing.SwingConstants; import java.awt.Color; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.UIManager; import javax.swing.ImageIcon; import java.awt.event.ContainerAdapter; import java.awt.event.ContainerEvent; import javax.swing.border.EtchedBorder; class pointer { public int prepointer; public int nextpoint; public int length; public int signal; pointer() { prepointer=0; nextpoint=0; length=0; signal=0; } } public class firstFit extends JFrame { private JPanel contentPane; private JTextField everySize; private JFrame frame; private JTextField workname; private JTextField count; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { firstFit frame = new firstFit(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public firstFit() { setResizable(false); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setBounds(300, 200, 559, 447); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JPanel panel = new JPanel(); panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, Color.DARK_GRAY, Color.PINK)); panel.setBounds(344, 10, 199, 399); contentPane.add(panel); panel.setLayout(null); JLabel label = new JLabel("进程大小"); label.setHorizontalAlignment(SwingConstants.CENTER); label.setForeground(Color.GRAY); label.setFont(new Font("微软雅黑", Font.BOLD, 15)); label.setBounds(15, 119, 65, 27); panel.add(label); label.setOpaque(true); everySize = new JTextField(); everySize.setHorizontalAlignment(SwingConstants.CENTER); everySize.setFont(new Font("微软雅黑", Font.BOLD, 15)); everySize.setColumns(10); everySize.setBounds(91, 121, 65, 21); panel.add(everySize); JLabel label_2 = new JLabel("进程名称"); label_2.setOpaque(true); label_2.setHorizontalAlignment(SwingConstants.CENTER); label_2.setForeground(Color.GRAY); label_2.setFont(new Font("微软雅黑", Font.BOLD, 15)); label_2.setBounds(15, 64, 65, 27); panel.add(label_2); workname = new JTextField(); workname.setHorizontalAlignment(SwingConstants.CENTER); workname.setFont(new Font("微软雅黑", Font.BOLD, 15)); workname.setColumns(10); workname.setBounds(91, 66, 65, 21); panel.add(workname); JLabel label_1 = new JLabel("KB"); label_1.setHorizontalAlignment(SwingConstants.CENTER); label_1.setForeground(Color.GRAY); label_1.setFont(new Font("微软雅黑", Font.BOLD, 15)); label_1.setBounds(155, 122, 26, 21); panel.add(label_1); JPanel panel_1 = new JPanel(); panel_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, Color.DARK_GRAY, Color.PINK)); panel_1.setBounds(10, 10, 324, 399); contentPane.add(panel_1); panel_1.setLayout(null); JLabel lblNewLabel_2 = new JLabel("模拟内存"); lblNewLabel_2.setForeground(Color.GRAY); lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER); lblNewLabel_2.setFont(new Font("微软雅黑", Font.BOLD, 18)); lblNewLabel_2.setBounds(64, 0, 118, 36); panel_1.add(lblNewLabel_2); JPanel panel_2 = new JPanel(); panel_2.setBorder(new EtchedBorder(EtchedBorder.LOWERED, Color.BLACK, Color.DARK_GRAY)); panel_2.setBounds(99, 50, 176, 312); panel_1.add(panel_2); panel_2.setLayout(null); final JPanel one = new JPanel(); one.setBorder(new EtchedBorder(EtchedBorder.RAISED, Color.BLACK, null)); one.setBounds(0, 0, 176, 54); panel_2.add(one); one.setLayout(null); JPanel two = new JPanel(); two.setBorder(new EtchedBorder(EtchedBorder.RAISED, new Color(0, 0, 0), null)); two.setBounds(0, 54, 176, 104); panel_2.add(two); two.setLayout(null); JPanel three = new JPanel(); three.setBorder(new EtchedBorder(EtchedBorder.RAISED, Color.BLACK, null)); three.setBounds(0, 158, 176, 154); panel_2.add(three); three.setLayout(null); final JLabel sizelabel = new JLabel("50KB"); sizelabel.setHorizontalAlignment(SwingConstants.CENTER); sizelabel.setFont(new Font("微软雅黑", Font.BOLD, 14)); sizelabel.setBounds(270, 68, 54, 19); panel_1.add(sizelabel); JLabel lblkb = new JLabel("100KB"); lblkb.setHorizontalAlignment(SwingConstants.CENTER); lblkb.setFont(new Font("微软雅黑", Font.BOLD, 14)); lblkb.setBounds(270, 144, 54, 19); panel_1.add(lblkb); JLabel lblkb_1 = new JLabel("150KB"); lblkb_1.setHorizontalAlignment(SwingConstants.CENTER); lblkb_1.setFont(new Font("微软雅黑", Font.BOLD, 14)); lblkb_1.setBounds(270, 292, 54, 19); panel_1.add(lblkb_1); final JLabel name[]=new JLabel[20]; for(int i=0;i<20;i++) { name[i]=new JLabel(); name[i].setFont(new Font("微软雅黑",Font.BOLD,12)); name[i].setHorizontalAlignment(SwingConstants.CENTER); panel_1.add(name[i]); } final JLabel blocks[]=new JLabel[300]; for(int i=0;i<50;i++) { blocks[i] = new JLabel(""); blocks[i].setOpaque(false); one.add(blocks[i]); } for(int i=50;i<150;i++) { blocks[i] = new JLabel(""); blocks[i].setOpaque(false); two.add(blocks[i]); } for(int i=150;i<300;i++) { blocks[i] = new JLabel(""); blocks[i].setOpaque(false); three.add(blocks[i]); } //设置前指针和后指针,以及空闲区的初始长度 final pointer pt[]=new pointer[3]; for(int i=0;i<3;i++) { pt[i]=new pointer(); } pt[0].prepointer=0; pt[0].nextpoint=50; pt[0].length=50; pt[0].signal=0; pt[1].prepointer=50; pt[1].nextpoint=150; pt[1].length=100; pt[1].signal=0; pt[2].prepointer=150; pt[2].nextpoint=300; pt[2].length=150; pt[2].signal=0; final JButton restartbutton = new JButton("重新开始"); final JButton startbutton = new JButton("开始分配"); startbutton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String str=everySize.getText(); int size=Integer.parseInt(str); String str1=count.getText(); int num=Integer.parseInt(str1); String str2=workname.getText(); int i; for(i=0;i<3;i++) { if(pt[i].length<size||pt[i].signal==1) { continue; } else { for(int j=0;j<size;j++) { if(pt[i].prepointer<50){ blocks[j+pt[i].prepointer].setBounds(2, j+2+pt[i].prepointer, 172, 1); blocks[j+pt[i].prepointer].setBackground(new Color(250,0,0)); blocks[j+pt[i].prepointer].setOpaque(true); } if(pt[i].prepointer<150&&pt[i].prepointer>=50) { blocks[j+pt[i].prepointer].setBounds(2, j+2+pt[i].prepointer-50, 172, 1); blocks[j+pt[i].prepointer].setBackground(new Color(0,250,0)); blocks[j+pt[i].prepointer].setOpaque(true); } if(pt[i].prepointer<300&&pt[i].prepointer>=150) { blocks[j+pt[i].prepointer].setBounds(2, j+2+pt[i].prepointer-150, 172, 1); blocks[j+pt[i].prepointer].setBackground(new Color(0,0,250)); blocks[j+pt[i].prepointer].setOpaque(true); } } pt[i].prepointer=pt[i].prepointer+size; pt[i].length=pt[i].length-size; break; } } if(i==3) {JOptionPane.showMessageDialog(frame, "没有合适的空闲分区!", null, JOptionPane.WARNING_MESSAGE);} else { name[num].setText(str2+" "+size+"KB"); name[num].setBounds(10, pt[i].prepointer+50-size, 79, 15); num++; count.setText(num+""); } } }); startbutton.setFont(new Font("微软雅黑", Font.BOLD, 15)); startbutton.setBounds(49, 178, 132, 34); panel.add(startbutton); restartbutton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { pt[0].prepointer=0; pt[0].nextpoint=50; pt[0].length=50; pt[0].signal=0; pt[1].prepointer=50; pt[1].nextpoint=150; pt[1].length=100; pt[1].signal=0; pt[2].prepointer=150; pt[2].nextpoint=300; pt[2].length=150; pt[2].signal=0; for(int i=0;i<300;i++) { blocks[i].setOpaque(false); blocks[i].setBackground(new Color(240,240,240)); } everySize.setText(""); for(int i=0;i<20;i++) { name[i].setText(""); } workname.setText(""); } }); restartbutton.setFont(new Font("微软雅黑", Font.BOLD, 15)); restartbutton.setBounds(49, 245, 132, 34); panel.add(restartbutton); count = new JTextField(); count.setEditable(false); count.setText("0"); count.setEnabled(false); count.setBounds(72, 253, 19, 15); panel.add(count); count.setColumns(10); count.setVisible(false); } }
四、实验总结
一开始在写程序的时候理解错了,导致后面写回收的时候出现了许多问题,导致回收那些代码出现了一点错乱,最后只能整个模块去回收,导致程序不完整。
在编写界面的时候由于对java界面的知识点了解相对比较少,导致在制作界面过程中遇到不少难题,最后是通过上网搜索相关知识和向老师或者同学请教才能解决。