java.lang.OutOfMemoryError:Java堆空间
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
有没有办法允许您在
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 aCollection
? 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.即使您有足够的内存,
ArrayList
也最多可以有Integer.MAX_VALUE
元素。Double.MAX_VALUE
远远超出了上述限制。在这种情况下,您在
add
期间内存不足,导致数组列表增长。Even if you had enough memory,
ArrayList
can have at mostInteger.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.您的代码无法工作的另一个原因是:
double
只能表示精确到大约 2^52 的整数 - 之后,i++
将不起作用,并且for
循环永远不会终止。永远不应该使用浮点变量作为循环计数器。请改用
int
或long
。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 thefor
loop will never terminate.You should never use floating-point variables as loop counters. Use
int
orlong
instead.您应该获取 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()
.您正在尝试分配 10^308 个值。这有很多价值。
You are trying to allocate of the order of 10^308 values. That's a lot of values.
增加堆的大小就可以了。只需使用以下参数运行该程序:
它会将堆大小增加到 512 MB。您可以指定任意数量:1g、2g 等等。
Increasing the size of the heap will do. Just run the program with this argument:
It will increase your heap size to 512 MB. You can specify as much as you want: 1g, 2g and so on.
在循环中:
如果有空间,
ArrayList
只会将值添加到ArrayList
中。如果没有,它将增加 ArrayList 的大小,然后继续添加。因此,您基本上所做的就是在创建此 ArrayList 时使用堆中分配的所有内存。如果你让你的
ArrayList
变小,你应该能够将它保存在内存中。In you loop:
An
ArrayList
will just add the value to theArrayList
if there is room. If not it will increase the size of theArrayList
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 yourArrayList
smaller you should be able to hold it in memory.