nikhilgupta86
nikhilgupta86

Reputation: 492

Java Heap not full but still getting error java.lang.OutOfMemoryError: Java heap space

I have this strange issue going on in my one of production application. I have checked the heap space on the server and do not found any anomalies (Please see below screen) but still i am getting the error below on few operations that my application does. Is it possible?

enter image description here

java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2271)
at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:118)
at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93)
at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:153)
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1876)
at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(ObjectOutputStream.java:1785)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1188)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)

I am suspecting the issue to be one of the list in the code that will be filled up by almost 2M records at once, but still not sure why this is happening as the heap is not showing full or used at maximum, but still getting this error. Is it possible? what are the scenarios possible, is there a way to prevent this. This is production application so i would not be able to share the code. Please help me out.

Upvotes: 2

Views: 1678

Answers (3)

nasukkin
nasukkin

Reputation: 2540

From java.lang.OutOfMemoryError:

Thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector. OutOfMemoryError objects may be constructed by the virtual machine as if suppression were disabled and/or the stack trace was not writable.

In short, the operation you are trying to accomplish requires a certain amount of memory on the heap (creating a new array with 2-million elements is going to require somewhere in the neighborhood of 8-16+ megabytes depending on your JVM implementation), and your JVM is saying that it simply doesn't have that much available. You don't see the memory spike above the available heap because the JVM stops that from happening before it happens.

Note that this is an Error as opposed to an Exception, hence why none of the javadoc for the stacktrace don't mention it and why you weren't expected to handle it as opposed to a checked exception.

Upvotes: 0

user207421
user207421

Reputation: 310957

It doesn't have to be full to throw OutOfMemoryError. It has to be in a condition where the current size plus the size of the next requested allocation exceeds the maximum. Here you are clearly copying a large enough array that allocating it would trip (has tripped) this condition.

Upvotes: 3

Daniel O
Daniel O

Reputation: 351

The function at the top of your stack trace that copies the array seems to be checking whether it has enough space for the entire array before beginning to copy. That would explain why the heap isn't filled when the error is thrown. Could the entire array being copied even fit in the heap?

Upvotes: -1

Related Questions