Java内存不足2D阵列 [英] Java Out of memory 2D Array
问题描述
我正尝试如下创建2D数组.
I am trying to create a 2D array as below.
int NUM_RECORDS = 100480507;
byte[][] completeArray = new byte[NUM_RECORDS][6];
拥有100480507 * 6 ~= 0.6 GB
请参见> 此 问题.
See this question as well.
但是此数组的创建将耗尽内存.我已经通过JVM args为我的java进程分配了4G.
But creation of this array runs out of memory. I have allocated 4G to my java process through JVM args.
如何解释?我在这里想念一些琐碎的东西吗?
How can that be explained? I am I missing some thing trivial here?
这是我的程序
public class MemTest {
public static void main(String[] args) {
int NUM_RECORDS = 100480507;
byte[][] completeArray = new byte[NUM_RECORDS][6];
System.out.println("Array created");
}
}
推荐答案
Each array has an overhead (for example, see IBM documentation on their overhead -> http://www.ibm.com/developerworks/java/library/j-codetoheap/index.html). In your case, you are creating 100480507 of them!
如果您将代码更改为" byte [] completeArray = new byte [ NUM_RECORDS * 6 ];",则根据您的理论,它应该占用相同的空间!但是,我相当确定这会起作用,因为开销最少.您也可以尝试"byte [] [] completeArray = new byte [6] [NUM_RECORDS] ;".而且应该也可以(减少开销).
If you change your code to "byte[] completeArray = new byte[NUM_RECORDS*6];", according to your theory, it should demand the same space! But, I am fairly certain this will work as there is least overhead. You could also try "byte[][] completeArray = new byte[6][NUM_RECORDS];" and that should work too (lesser overhead).
我知道这不会解决您的问题-但我希望这可以使您对间接费用有所了解.
I know this will not solve your issue - but I hope this will give you some perspective on the overhead.
这篇关于Java内存不足2D阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!