How do I make a large 3D array without running out of memory?

时光毁灭记忆、已成空白 提交于 2019-12-11 20:25:31

问题


I have the following method:

public static void createGiantArray(int size) {
    int[][][] giantArray = new int[size][size][size];
}

When I call it with a size of 10,000 like so:

createGiantArray(10000);

I get the following error:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

How can I create an array that is 10,000 x 10,000 x 10,000 while bypassing the memory exception?

(I am aware my current method is pointless and loses scope. I didn't post the extra code that goes with it.)


回答1:


If the data is going to be relatively sparse, then you can use a different data structure, like described here or here.




回答2:


Not sure if you're realising that a 3 dimensional array of 10,000 by 10,000 by 10,000 integers (4 bytes each) amounts to 3725Gb! You can increase your heap size with the -Xmx flag, but that amount is still humungous.

You need to change the design of your program.




回答3:


Basically you can't mantain in memory a structure of that dimension. Each int occupy 4 bytes. So you have 10.000 x 10.000 x 10.000 x 4 bytes = 4.000.000.000.000 bytes that is around 4 terabyte. You need to mantain your data on a database (quite big database) and access them as needed. A secondary possibility is to mantain the data in a distributed memory system like a distributed hashtable over many servers. For example 4000 servers with 1 gigabyte of ram.



来源:https://stackoverflow.com/questions/29238953/how-do-i-make-a-large-3d-array-without-running-out-of-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!