Java OutOfMemoryError message changes when trying to create Arrays of different sizes
Posted
by Gordon
on Stack Overflow
See other posts from Stack Overflow
or by Gordon
Published on 2009-12-10T15:00:26Z
Indexed on
2010/05/09
3:58 UTC
Read the original article
Hit count: 334
In the question by DKSRathore How to simulate the Out Of memory : Requested array size exceeds VM limit some odd behavior was noted when creating an arrays.
When creating an array of size Integer.MAX_VALUE an exception with the error java.lang.OutOfMemoryError Requested array size exceeds VM limit
was thrown.
However when an array was created with a size less than the max but still above the virtual machine memory limit the error message read java.lang.OutOfMemoryError: Java heap space
.
Testing further I managed to narrow down where the error messages changes.
long[] l = new long[2147483645];
exceptions message reads - Requested array size exceeds VM limit
long[] l = new long[2147483644];
exceptions message reads - Java heap space errors
I increased my virtual machine memory and still produced the same result.
Has anyone any idea why this happens?
Some extra info: Integer.MAX_VALUE = 2147483647.
Edit: Here's the code I used to find the value, might be helpful.
int max = Integer.MAX_VALUE;
boolean done = false;
while (!done) {
try {
max--;
// Throws an error
long[] l = new long[max];
// Exit if an error is no longer thrown
done = true;
} catch (OutOfMemoryError e) {
if (!e.getMessage().contains("Requested array size exceeds VM limit")) {
System.out.println("Message changes at " + max);
done = true;
}
}
}
© Stack Overflow or respective owner