Reputation: 113
I have int K as an input (K will be between 0 and 1000000 (one million), inclusive) and I would like then to create a matrix of integers with height and width 2 times longer than K number, plus 1 element more.
Example if K=3, my matrix would be matrix[3*2+1][3*2+1]=matrix[7][7] if K=178754, my matrix would be matrix[357503][357503]
I got an java.lang.OutOfMemoryError: Java heap space exception in thread "main"
int height;
int width;
int [][] matrix;
public int getKthNumber(int N, int K, String direction) {
this.height = (K * 2) + 1;
this.width = (K * 2) + 1;
****matrix = new int[this.width][this.height];**** // error here
Does anyone knows how to solve this little problem since I believe that the whole other code is OK ??
Upvotes: 1
Views: 789
Reputation: 122006
my matrix would be matrix[357503][357503]
Yes .That's all enough to get out of memory. JVM tries to allocate memory for 357503X357503
of integers, where as each integer carries 32 bits which resulting ~476GB almost.
What actually you are trying to do ?
Upvotes: 4
Reputation: 10717
In a 32 bit system you can have up to 2GB . On a 64 bit system you can get more.
Command line flags are:
java -Xmx2g myprogram
But in your case it seems that this will not be enough, since 357503*357503*4/1024/1024/1024 (4 for bytes for integer) which is about 476G, so I think that you should rethink your solution.
Upvotes: 0