Arun George
Arun George

Reputation: 1219

Creating a huge 2D array in Java

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?

Upvotes: 0

Views: 405

Answers (2)

Florent TESTE
Florent TESTE

Reputation: 11

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 !

Upvotes: 1

karthikdivi
karthikdivi

Reputation: 3643

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]

Upvotes: 1

Related Questions