使用链表实现内存空间分配和释放



这是一个关于使用链表实现内存管理的家庭作业问题。每个内存进程请求特定大小的内存,该内存必须连续大到足以容纳内存,然后分配进程。当作业终止时,其允许的内存变为空闲内存。下面是我的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)中,将为P4分配100k的空间,即使早先由P2容纳的空间现在是空闲的,因此P4可以包含在早先P2的空间中。

如何使P4加入到P2的空闲空间

deleteProcess方法删除对要删除的PartitionNode对象的引用,从而允许垃圾收集器释放内存。但是,Java的垃圾收集器不能保证这将立即发生,或者在删除对对象的所有引用后的一段时间内发生。

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

我希望我理解了你的问题,这对你有帮助。

在添加新插槽到链接列表的末尾之前,您必须搜索空闲插槽。参考"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();
        }
    }

相关内容

  • 没有找到相关文章

最新更新