I am writing simple program in java to create 2 int arrays of 1 billion size. I ran this program with -Xms10G, i.e. 10GB of memory still I got OOM error. Below is the snippet.
public class TestBigIntArraySize {
public static int arraySize = 1000_000_000;
public static int [] firstArray = new int[arraySize];
public static int [] secondArray = new int[arraySize];
public static void main(String[] args) {
System.out.println(1000_000_000 * Integer.SIZE);
}
}
As far as I can think the memory used for 1 billion int array would be System.out.println(1000_000_000 * Integer.SIZE); which returns 1,935,228,928 which is lesser than 2GB. So my programs total requirements would be max 4GB.
I get error even if I create arrays in a method call and return array or static (like below) or inside main(). The memory that is required for it work is 12G which is 3 times of what I expected. I am using oracle java : jdk1.8.0_201
I tried the option -Xms10G -XX:NewRatio=1 --- which Worked.
But I want to decrease my memory footprint further.
I tried option to giving more memory to eden by -Xms9G -XX:NewRatio=0.5
but java complains illegal argument.
I tried option to directly allocate array to old gen by -Xms9G -XX:NewRatio=1 -XX:PretenureSizeThreshold=10000
. But this also give OOM.
It's just an experimental project and I am just manipulating the array's in place. I would like to do it in smallest possible memory. Can someone suggest how to go about it? What java options and why?