java.lang.OutOfMemoryError : Java 堆空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7013384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:16:57  来源:igfitidea点击:

java.lang.OutOfMemoryError : Java heap space

javalistcollectionsheapout-of-memory

提问by Wassim AZIRAR

I was playing with some examples of Collections from Oracle website

我正在玩一些来自 Oracle 网站的 Collections 示例

public class Timing {

    public static void method(){

        List numbers = new ArrayList();

        for (double i = 1; i <= Double.MAX_VALUE; i++)
        numbers.add(new Double(i));

        Collections.shuffle(numbers);
        List winningcombination = numbers.subList(0, 10);
        Collections.sort(winningcombination);
    }

    public static void main(String[] args)
    {
        long start = System.currentTimeMillis();
        method();
        long end = System.currentTimeMillis();
        System.out.println("time elapsed : " + (end-start));
    }
}

I tried to see how long it will take to do it for Double.MAX_VALUE. And I got this :

我试着看看为 Double.MAX_VALUE 做这件事需要多长时间。我得到了这个:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Unknown Source)
    at java.util.Arrays.copyOf(Unknown Source)
    at java.util.ArrayList.ensureCapacity(Unknown Source)
    at java.util.ArrayList.add(Unknown Source)

I there a way to fix this ?

我有办法解决这个问题吗?

回答by Ernest Friedman-Hill

Is there a way to allow you to create and store Double.MAX_VALUEobjects in a Collection? No. There's not that much RAM on Earth. Double.MAX_VALUEis about 2 times ten to the 308th power: that's 2 followed by over 300 zeros. Give Best Buy a call, see how much they'd charge to put that in your computer.

有没有一种方法可以让您Double.MAX_VALUE在一个中创建和存储对象Collection?不。地球上没有那么多内存。Double.MAX_VALUE大约是 10 的 308 次方的 2 倍:即 2 后跟超过 300 个零。给百思买打个电话,看看他们把它放进你的电脑要多少钱。

回答by Dilum Ranatunga

Even if you had enough memory, ArrayListcan have at most Integer.MAX_VALUEelements. Double.MAX_VALUEfar exceeds said limit.

即使你有足够的内存,ArrayList最多也可以有Integer.MAX_VALUE元素。Double.MAX_VALUE远远超过上述限制。

In this case, you ran out of memory during an addthat caused the array list to grow.

在这种情况下,您在add导致数组列表增长的过程中内存不足。

回答by Michael Borgwardt

Yet another reason why your code cannot work: doublecan only represent integers exactly up to about 2^52 - after that, i++will have no effect and the forloop will never terminate.

您的代码无法工作的另一个原因:double只能精确表示大约 2^52 的整数 - 之后,i++将无效并且for循环永远不会终止。

You should never use floating-point variables as loop counters. Use intor longinstead.

你永远不应该使用浮点变量作为循环计数器。使用intlong代替。

回答by Marcelo

Instead of doing what you are currently doing, you should just obtain 10 random doubles, add them to an ArrayList and sort it. That is basically what your method is doing.

而不是做你目前正在做的事情,你应该只获得 10 个随机双打,将它们添加到一个 ArrayList 并对其进行排序。这基本上就是你的方法所做的。

To obtain a random double, look at Random.nextDouble().

要获得随机双精度值,请查看Random.nextDouble()

回答by djna

You are trying to allocate of the order of 10^308 values. That's a lot of values.

您正在尝试分配 10^308 个值的顺序。这是很多价值。

回答by Caius Brindescu

Increasing the size of the heap will do. Just run the program with this argument:

增加堆的大小就可以了。只需使用此参数运行程序:

-Xmx512m

It will increase your heap size to 512 MB. You can specify as much as you want: 1g, 2g and so on.

它会将您的堆大小增加到 512 MB。您可以指定任意数量:1g、2g 等。

回答by Arthur Neves

for (double i = 1; i <= Integer.MAX_VALUE; i++)
        numbers.add(new Double(i));

回答by J Lundberg

In you loop:

在你循环中:

for (double i = 1; i <= Double.MAX_VALUE; i++)
    numbers.add(new Double(i));

An ArrayListwill just add the value to the ArrayListif there is room. If not it will increase the size of the ArrayListand then continue adding.

如果有空间,AnArrayList只会将值添加到ArrayList。如果不是它会增加的大小,ArrayList然后继续添加。

So what you are basically doing is using all the memory allocated in your heap when you are creating this ArrayList. If you make your ArrayListsmaller you should be able to hold it in memory.

因此,您所做的基本上是在创建它时使用堆中分配的所有内存ArrayList。如果你让你的ArrayList更小,你应该能够将它保存在内存中。