简体   繁体   English

内存不足:Java堆空间

[英]Out of Memory: Java heap space

I am doing a homework assignment on Big O Notation. 我正在按“大O符号”进行作业。 We need to create lists with ever increasing number of integers and then time how long it takes to sort the list to determine which Big O Collection.sort() is. 我们需要创建数量不断增加的整数列表,然后确定对列表进行排序以决定哪个Big O Collection.sort()需要花费多长时间。

I have written some code to do this and produce data on how long the sort method takes over the course of a few iterations of each list size. 我已经编写了一些代码来执行此操作,并生成有关sort方法在每个列表大小的几次迭代过程中需要花费多长时间的数据。

I am able to sort a list of 50,000 integers, but if I try to do the exact same operation again I will run out of memory. 我能够对50,000个整数进行排序,但是如果我再次尝试执行完全相同的操作,则会耗尽内存。 I think the garbage collector should reclaim my memory, so in theory there should be no problem repeating the operation. 我认为垃圾收集器应该回收我的内存,因此从理论上讲,重复该操作应该没有问题。

I've read about setting my large variables to null, and not storing references to my large lists outside the for loop block. 我已经读过有关将大变量设置为null,并且不将对大列表的引用存储在for循环块之外的内容。 I think there is no reason any reference to my integer list should be kept alive. 我认为没有任何理由应保留我的整数列表。

What am I doing wrong that garbage collector cannot reclaim my memory? 垃圾收集器无法回收我的内存,这是我做错了什么?

        private static TreeMap<Integer, ArrayList<Long>> doExperiment(int iterations, int step, int trials, List listType) {
    TreeMap<Integer, ArrayList<Long>> results = new TreeMap();
    for (int i = 1; i <= iterations; i++) {
        // Data size ranges from 1000 - 50,0000 if step = 1000 and iterations = 50
        int dataSize = i * step;
        ArrayList<Long> trialResults = new ArrayList<Long>();
        for (int j = 1; j <= trials; j++) {
            // This may be LinkedList, ArrayList depending on the parameter.
            List thisList = listType;
            // dataSize works up to 50,000 Integers.
            Testing t = new Testing(dataSize, thisList);
            long nanos = t.timedSort();
            // Prints a formatted string to standard output.
            processResults(nanos, j, trials, i, iterations);
            // The large list exists only within this Testing instance. Set it to null
            t = null;
            // Please, garbage collection Gods...
            System.gc();
        }
        results.put(dataSize, trialResults);
    }
    return results;
}

System.gc(); - not guaranty to launch garbage collector -不保证启动垃圾收集器

List thisList = listType; - copies reference (I guess this is mistake) so when you do Testing t = new Testing(dataSize, thisList); -复制参考(我猜这是错误的),所以当您进行Testing t = new Testing(dataSize, thisList); - this is actually the same as Testing t = new Testing(dataSize, listType); -这实际上与Testing t = new Testing(dataSize, listType);

Probably you want to do : List thisList = new ArrayList(listType); 可能您想做: List thisList = new ArrayList(listType); - Yes. -是的 This create new list but in this case Testing will operate new list but not with the same one. 这将创建一个新列表,但在这种情况下,测试将操作新列表,但不能使用同一列表。

ArrayList<Long> trialResults = new ArrayList<Long>(); // you create new list
....
results.put(dataSize, trialResults); // do you want to put empty list in the result? 
public static void main(String[] args) {
        // TODO code application logic here
        doExperiment(50000, 5, "ArrayList");
        doExperiment(50000, 5, "LinkedList");

    }

    private static void doExperiment(int iterations, int trials, String listType) {
        List list = null;
        List<Long> holdStartTimes = new ArrayList();
        List<Long> holdEndTimes = new ArrayList();

        switch(listType)
        {
            case"ArrayList":
                list = new ArrayList();
                break;
            case "LinkedList":
                list = new LinkedList();
                break;          
        }

        for(int t = 0; t < trials; t++)
        {
            Random random = new Random();
            //create list with random ints
            for(int i = 0; i < iterations; i++)
            {
                list.add(random.nextInt(50001));//random number in range from 0 to 50000
            }

            final long startTime = System.currentTimeMillis();//start Timer
            holdStartTimes.add(startTime);
            Collections.sort(list);//sort list
            final long endTime = System.currentTimeMillis();//end Timer
            holdEndTimes.add(endTime);
        }

        //sum times
        long sum = 0;
        for(int i = 0; i < holdStartTimes.size(); i++)
        {
            sum = sum + (holdEndTimes.get(i) - holdStartTimes.get(i));
        }

        System.out.println("Sorting " + listType + " with " + iterations + " iterations and " + trials + " trials takes " + sum + " milliseconds.");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM