使用链接List的内存空间分配和释放实现

时间:2014-06-24 06:21:30

标签: java memory memory-management linked-list

这是与使用链接列表的内存管理实现相关的功课问题。 每个内存处理特定大小的内存请求,这些内存必须连续大到足以适合内存,然后分配进程。当作业终止时,其允许的内存变为空闲。 这是我的java代码:

 public class PartitionNode{
    int beginAddress;
    int endAddress;
    boolean holeFree;
    int processId;
    PartitionNode next;

public PartitionNode(int begin,int end){
     beginAddress=begin;
     endAddress=end;
     holeFree=true;
     processId=-1;
     next=null;


}

public PartitionNode(){}
public PartitionNode(int begin,int end,int i){
    beginAddress=begin;
    endAddress=end;
    holeFree=false;
    processId=i;

}

}

public class Partition{
    private  PartitionNode head;
    public PartitionNode current;

public  int begin;
public int end;
public PartitionNode newPartition;
private Partition(int beginAddress,int endAddress){
    head=new PartitionNode(beginAddress,endAddress);
    begin=beginAddress;
    end=endAddress;
    current=head;
}

public void addProcess(int size,int id){
    if((current.endAddress-current.beginAddress>=size)&& current.holeFree==true){
        newPartition=new PartitionNode(current.beginAddress,current.beginAddress+size-1,id);
        current.next=newPartition;
        newPartition.next=refresh();

        System.out.println("beginAddress"+newPartition.beginAddress);
        System.out.println("endAddress"+newPartition.endAddress);

    }

}

 public void print(){
    PartitionNode temp=head;


    while(temp.next!=null){
        System.out.println(temp.processId);
        temp=temp.next;
    }
 }


public   PartitionNode refresh(){
    current=new PartitionNode(newPartition.endAddress+1,end);
        return current;

}
public void deleteProcess(int process){
    PartitionNode temp=head;
    PartitionNode temp2=head;

    while(temp.next!=null){
        if(temp.processId==process){

            temp2.next=temp.next;
            break;
        }
        temp2=temp;
        temp=temp.next;
    }

}



public static void main (String args[]){
    Partition p=new Partition(300,3000);
    p.addProcess(500,1);

    p.addProcess(800,2);

    p.addProcess(400,3);
    p.deleteProcess(2);
    p.print();


}


}

如果我按照

进行处理
  • 分配P1 500k
  • 分配P2 800k
  • 分配P3 400k
  • 终止P2
  • 分配P4 100k

根据我的代码,在剩余的1700-3000空间内(假设总内存大小为3000k),将为p分配100k空间,即使P2先前容纳的空间现在是空闲的,因此P4可以包含在早期p2的空间。

如何使其工作以便将P4添加到P2的现在可用空间

2 个答案:

答案 0 :(得分:0)

您的deleteProcess方法删除了您要删除的PartitionNode对象的引用,从而允许垃圾收集器释放内存。但是,Java的垃圾收集器并不保证在删除对象的所有引用后立即或在设定的时间段内发生这种情况。

这对你来说意味着内存没有被释放,你无法控制何时会发生这种情况。

我希望我理解你的问题,这会有所帮助。

答案 1 :(得分:0)

在将新插槽添加到链接列表的末尾之前,您必须搜索空闲插槽。请参阅我们搜索空闲插槽的“searchSlotAndInsert”。

尝试以下代码:

    public class Partition {
        private PartitionNode head;
        public PartitionNode current;

        public int begin;
        public int end;
        public PartitionNode newPartition;

        private Partition(int beginAddress, int endAddress) {
            head = new PartitionNode(beginAddress, endAddress);
            begin = beginAddress;
            end = endAddress;
            current = head;
        }

        public void addProcess(int size, int id) {
            boolean isProcessInserted = searchSlotAndInsert(size, id); 
            if (!isProcessInserted && (current.endAddress - current.beginAddress >= size)
                    && current.holeFree == true) {
                newPartition = new PartitionNode(current.beginAddress,
                        current.beginAddress + size - 1, id);
                current.next = newPartition;
                newPartition.next = refresh();
            } 
        }

        public void print() {
            PartitionNode temp = head;
            while (temp.next != null) {
                System.out.println(temp.next.processId);
                temp = temp.next.next;
            }
        }

        public PartitionNode refresh() {
            current = new PartitionNode(newPartition.endAddress + 1, end);
            return current;
        }

        public void deleteProcess(int process) {
            PartitionNode temp = head;
            PartitionNode temp2 = head;
            while (temp.next != null) {
                if (temp.processId == process) {
                    temp2.next = temp.next.next;
                    if(temp2.next == null){
                        current = temp2;
                    }
                    break;
                }
                temp2 = temp;
                temp = temp.next;
            }
        }
//Search for free slot
        public boolean searchSlotAndInsert(int size, int id){
            PartitionNode temp2 = head;
            PartitionNode temp = head.next;
            while (temp != null) {
                if (temp2.beginAddress != temp.beginAddress) {
                    break;
                }
                temp2 = temp.next;
                temp = temp.next.next;
            } 
            if(temp != null && temp.beginAddress-temp2.beginAddress >= size){
                PartitionNode newPartition1 = new PartitionNode(temp2.beginAddress,
                        temp2.beginAddress + size - 1, id);
                temp2.next = newPartition1;
                PartitionNode temp3 = new PartitionNode(newPartition1.endAddress + 1, end);
                newPartition1.next = temp3;
                temp3.next = temp;
                return true;
            }
            return false;
        }

        public static void main(String args[]) {
            Partition p = new Partition(300, 4000);
            p.addProcess(500, 1);
            p.addProcess(800, 2);
            p.addProcess(400, 3);
            p.deleteProcess(2);
            p.deleteProcess(3);
            p.addProcess(1100, 4);
            p.addProcess(800, 5);
            p.addProcess(100, 6);
            p.deleteProcess(1);
            p.deleteProcess(5);
            p.deleteProcess(6);
            p.addProcess(900, 7);
            p.addProcess(100, 8);
            p.addProcess(500, 9);
            p.print();
        }
    }