Reputation: 21
I'm beginner in java and I tried to make a code which sums two matrixes, but I have the error :
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at Main.main(Main.java:11)
I tried all this commands :
$ java Main
$ java Main -Xmx2048m
$ java Main -Xms1024m
$ java Main -Xms1024m -Xmx2048m
$ java -Xmx2048m Main
$ java -Xms1024m Main
$ java -Xms1024m -Xmx2048m Main
But all give the same error message...
This is the code :
public class Main
{
public static void main(String[] args)
{
final int n = 10000;
double A[][] = new double[n][n];
double B[][] = new double[n][n];
double S[][] = new double[n][n];
int i,j;
for (i = 0; i<n; i++)
{
for (j = 0; j<n; j++)
{
A[i][j] = 1.0d / ((double) i*j);
B[i][j] = 1.0d / ((double) i*j);
}
}
for (i = 0; i<n; i++)
{
for (j = 0; j<n; j++)
{
S[i][j] = A[i][j] + B[i][j];
}
}
}
}
So as you can see, this code need ~ 3x8x10000 B = 240 000 B < 2048 MB. My java's version :
$ java -version
openjdk version "11.0.4" 2019-07-16
OpenJDK Runtime Environment (build 11.0.4+11-post-Ubuntu-1ubuntu218.04.3)
OpenJDK 64-Bit Server VM (build 11.0.4+11-post-Ubuntu-1ubuntu218.04.3, mixed mode, sharing)
Upvotes: 1
Views: 451
Reputation: 952
I am able to run your code without any Xms or Xmx setting. My Laptop is having 16 GB Ram.
Upvotes: 0
Reputation: 2166
The flags work, but your math does not check out.
You have 3
arrays, each with 10 000 * 10 000 * 8B
values.
For that you need 3 * 10 000 * 10 000 * 8 Bytes
... which is 2289 MB
.
That is already more than 2048
.
To add to that, each of those 3
arrays contains 10 000
references... but the memory overhead of that is minuscule (less than 1MB
) compared to the values.
Upvotes: 1