【发布时间】:2011-08-11 16:25:06
【问题描述】:
我写了一个小程序来检查按行或列循环遍历二维数组的时间,因为我记得一种方法对于大型数据集要快得多,但我不记得哪种方法了。不过,当我运行我的程序时,我遇到了一个有趣的错误。
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at testing.ForLoopTest.main(ForLoopTest.java:10)
现在在我的测试中,我使用了一个 int[10000][10000],它在 eclipse 中运行,具有默认的堆设置。我知道我可以增加堆大小,但我的直觉告诉我这应该运行得很好,而我不需要这样做。这是正常的吗?我的代码中是否有一些我没有看到的愚蠢的东西?
这是我的代码
public static void main(String[] args){
final int arrSize = 10000;
long start = 0;
long rowFirstTime = 0;
long colFirstTime = 0;
int[][] arr = new int[arrSize][arrSize];
start = System.currentTimeMillis();
for(int x = 0; x < arrSize; x++){
for(int y = 0; y < arrSize; y++){
arr[x][y] = -1;
}
}
rowFirstTime = System.currentTimeMillis() - start;
start = System.currentTimeMillis();
for(int y = 0; y < arrSize; y++){
for(int x = 0; x < arrSize; x++){
arr[x][y] = 0;
}
}
colFirstTime = System.currentTimeMillis() - start;
System.out.println("row time is " + rowFirstTime);
System.out.println("col time is " + colFirstTime);
}
错误发生在数组初始化时,它没有进入 for 循环。
谢谢, zdevex
【问题讨论】:
-
您正在使用 100,000,00(1 亿)个整数,您确定默认堆大小会支持吗?
标签: java arrays heap-memory