I am trying to create a huge 2D array.
String[][] arr = new String[100000][100000];
But on execution, I get java.lang.OutOfMemoryError: Java heap space
Other than increasing my heap space, how do I prevent getting this exception?
Are you really going to use 100000 X 100000 positions, If not and you are not sure about the max limit you can start with List of Lists
declare like this List<List<String>> arr = new ArrayList<List<String>>();
When ever you want to call you need to do this arr.get(0).get(1); instead arr[0][1]
-Xmx20g, see stackoverflow.com/questions/14763079/… But you should consider, that this won't be very fast. You should consider using a different algorithm or some kind of distributed cache / in memory DB.