0

I am trying to do similar kind of stuff but getting heap space error. What is the solution for this?

String mata[][] = new String[100000][100000];    
String matb[][] = new String[100000][100000];   
int j=0;        

for (int i = 0; i < 5000; i++) {        
    if((i%2) == 0){    
        mata[i][j] = "2";
    }else{
        matb[i][j] = "3";   
    }           
    if((i%3) == 2){   
        mata[i][j] = "2";  
    }else{  
        matb[i][j] = "3";  
    }           
 }
Lucky
  • 16,787
  • 19
  • 117
  • 151
  • 2
    You're allocating two arrays that hold up to 10 billion strings each. You might then expect that this would take up a fair bit of memory...[so give it some more.](http://stackoverflow.com/q/1565388/3419894) – JonK Oct 22 '15 at 08:40
  • 1
    there is no practical way you can run a code that wants to create 100k*100k=10 bio String objects. This would require already 80GB (assuming a 64-bit platform) of RAM just for the array. Perhaps you need to store only byte and not strings (back down to 10GB which is still a lot) – secolive Oct 22 '15 at 08:45
  • But aren't they variables stocked in the arrays pointing to the String pool? @secolive – Yassin Hajaj Oct 22 '15 at 08:59
  • Yes but still 10 bio references, each 8-byte=80GB. – secolive Oct 22 '15 at 09:42

2 Answers2

0

You're getting this error because default heap size is not sufficient to hold 2 x 2d array of size 10000 x 10000

You can increase the size of heap size using -Xms64m -Xmx128m

Xms starting heap
Xmx max heap

for perm gen space -XX:MaxPermSize=128m

Example java -Xms64m -Xmx128m -jar MyJar.jar

Saravana
  • 12,647
  • 2
  • 39
  • 57
0

it will fail with java.lang.OutOfMemoryError: Java heap space Increase heap size to fix this. You can try something like this

java -Xmx6g yourprogram
Community
  • 1
  • 1
RockAndRoll
  • 2,247
  • 2
  • 16
  • 35