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

2019-09-04 19:36发布

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.)

3条回答
你好瞎i
2楼-- · 2019-09-04 19:47

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

查看更多
冷血范
3楼-- · 2019-09-04 20:04

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.

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-09-04 20:09

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.

查看更多
登录 后发表回答