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.)
If the data is going to be relatively sparse, then you can use a different data structure, like described here or here.
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.
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.