In the DKSRathore question How to simulate Out Of Memory: the size of the requested array exceeds the VM limit , some odd behavior was noted when creating the arrays.
When creating an array of size Integer.MAX_VALUE, an exception was thrown with an error java.lang.OutOfMemoryError Requested array size exceeds VM limit.
However, when an array was created with a size less than max, but still above the virtual machine memory limit, an error message is read java.lang.OutOfMemoryError: Java heap space.
Further testing, I was able to narrow the scope of error messages.
long[] l = new long[2147483645];
The exception message reads: "Required array size exceeds VM limit"
long[] l = new long[2147483644];
The exception message reads "Java Heap Space Errors"
- .
- , ?
:
Integer.MAX_VALUE = 2147483647
: , :
int max = Integer.MAX_VALUE;
boolean done = false;
while (!done) {
try {
max--;
long[] l = new long[max];
done = true;
} catch (OutOfMemoryError e) {
if (!e.getMessage().contains("Requested array size exceeds VM limit")) {
System.out.println("Message changes at " + max);
done = true;
}
}
}