java.lang.OutOfMemoryError:Java堆空间

发布于 2024-11-29 09:45:23 字数 1000 浏览 1 评论 0原文

我正在使用 Oracle 网站上的一些集合示例,

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));
    }
}

我试图看看为 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 was playing with some examples of Collections from Oracle website

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 :

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

夏末的微笑 2024-12-06 09:45:23

有没有办法允许您在 Collection 中创建和存储 Double.MAX_VALUE 对象?不,地球上没有那么多 RAM。 Double.MAX_VALUE 大约是 2 乘以 10 的 308 次方:即 2 后跟超过 300 个零。给百思买打电话,看看他们将其放入您的计算机需要多少钱。

Is there a way to allow you to create and store Double.MAX_VALUE objects in a Collection? No. There's not that much RAM on Earth. Double.MAX_VALUE is 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.

萌能量女王 2024-12-06 09:45:23

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

在这种情况下,您在 add 期间内存不足,导致数组列表增长。

Even if you had enough memory, ArrayList can have at most Integer.MAX_VALUE elements. Double.MAX_VALUE far exceeds said limit.

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

不必了 2024-12-06 09:45:23

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

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

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

You should never use floating-point variables as loop counters. Use int or long instead.

暮倦 2024-12-06 09:45:23

您应该获取 10 个随机双精度数,将它们添加到 ArrayList 中并对其进行排序,而不是执行当前正在执行的操作。这基本上就是您的方法正在做的事情。

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

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.

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

遥远的她 2024-12-06 09:45:23

您正在尝试分配 10^308 个值。这有很多价值。

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

浮生未歇 2024-12-06 09:45:23

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

-Xmx512m

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

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.

七月上 2024-12-06 09:45:23
for (double i = 1; i <= Integer.MAX_VALUE; i++)
        numbers.add(new Double(i));
for (double i = 1; i <= Integer.MAX_VALUE; i++)
        numbers.add(new Double(i));
拒绝两难 2024-12-06 09:45:23

在循环中:

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

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

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

In you loop:

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

An ArrayList will just add the value to the ArrayList if there is room. If not it will increase the size of the ArrayList and then continue adding.

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 ArrayList smaller you should be able to hold it in memory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文