I have a program where I will be using a very large short[]
array:
import java.lang.Math;
public class HPTest {
public static void main(String[] args) {
int n = 30;
short[] a = new short[(int)Math.pow(2,n)];
}
}
As far as I know, a short[]
array should use 2 bytes per element, and so an array with 2^30 elements should need about 2 GiB of RAM.
In order to run the program, I therefore tried
java -Xms2000m HPTest
but still got a heap space error. Even at 3000m
I got the same error, but at 4000m
it worked.
Any ideas as to why I had to go so far above the estimated limit of 2000m
?
EDIT:
As has been pointed out by many users, I made a very embarrassing error in declaring that a short needs 1 byte rather than 2 bytes. The question then should be why it doesn't suffice with 2000m
.