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?

3
  • 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. Commented May 7, 2016 at 6:49
  • Do you actually have that much RAM? If yes, you can try to increase the heap size -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. Commented May 7, 2016 at 6:58
  • @Mortiz Can you suggest an alternate algorithm for storing the huge data? Commented May 7, 2016 at 9:47

2 Answers 2

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]

Sign up to request clarification or add additional context in comments.

1 Comment

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.