I want to create a large int array that almost fills all the memory available to the JVM. Take this code, for example:
final int numBuffers = (int) ((runtime.freeMemory() - 200000L) / (BUFFER_SIZE));
System.out.println(runtime.freeMemory());
System.out.println(numBuffers*(BUFFER_SIZE/4)*4);
buffers = new int[numBuffers*(BUFFER_SIZE / 4)];
When launched with a heap size of 10M, this throws an OutOfMemoryException, despite the output from printlns:
9487176
9273344
I understand that the array will have some overhead, but not 200k, of course? Why can't Java allocate memory for what it claims to have enough space? I have to set this constant, which is subtracted by something around 4M, before Java starts it (by the time printlns look more similar: 9487176 5472256)
Even more confusing if I replace the buffers with a 2D array:
buffers = new int[numBuffers][BUFFER_SIZE / 4];
200k, , - ( 2D- , 1D-, ).
?