【发布时间】:2011-08-30 05:32:58
【问题描述】:
我尝试创建一个整数数组(我尝试使用自己的对象,但使用 int 也是如此),大小为 3000 万。我不断收到“OutOfMemoryError:Java 堆空间”
Integer [] index = new Integer[30000000];
for (int i = 0 ; i < 30000000 ; i++){
index[i] = i;
}
我使用“Runtime.getRuntime().totalMemory()”和“maxMemory()”检查了总堆空间 看到我从 64 MB 开始,最大值为 900+ MB,在运行期间我在堆上达到 900+ 并粉碎。
现在我知道 Integer 需要 4 个字节,所以即使我乘以 30*4*1000000,我仍然应该只得到大约 150-100 兆。
如果我尝试使用原始类型,例如 int,它可以工作。
我该如何解决?
【问题讨论】:
-
用更多的堆内存运行程序。
标签: java memory heap-memory