0

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?

Arun George
  • 1,167
  • 3
  • 15
  • 29
  • What is your data source? Do you really need all the data at the same time for processing? Otherwise you should consider writing data to a file, to free up space, adapt your processing algorithms to work on data batches, filter while writing, etc. – Frederic Klein May 07 '16 at 06:49
  • Do you actually have that much RAM? If yes, you can try to increase the heap size `-Xmx20g`, see http://stackoverflow.com/questions/14763079/what-are-the-xms-and-xmx-parameters-when-starting-jvms#14763095 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. – Moritz Petersen May 07 '16 at 06:58
  • @Mortiz Can you suggest an alternate algorithm for storing the huge data? – Arun George May 07 '16 at 09:47

2 Answers2

1

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]

karthikdivi
  • 3,466
  • 5
  • 27
  • 46
  • I am sure about the max limit. I can use a list, but while inserting, at some point, I would again end up out of memory. – Arun George May 07 '16 at 06:01
1

You can't do that Oo It make 100000*100000 = 10.000.000.000 datas for your array !

You are out of your memory !!

Reduce the two number, i don't think that you need this much memory !

You can go on a :

String[][] arr = new String[14000][14000];

I tried and it works !