James Raitsev
James Raitsev

Reputation: 96381

java.lang.OutOfMemoryError, when is memory reclaimed?

Once this exception is thrown, is object that caused this thrown out of memory? Does it happen right away?

In other words, if i am adding objects to a list, at some point this can no longer happen and OOM error is thrown. At that time, does anything happen to the list itself?

java.lang.OutOfMemoryError: Java heap space

Upvotes: 0

Views: 448

Answers (3)

LuxuryMode
LuxuryMode

Reputation: 33741

According to http://docs.oracle.com/javase/6/docs/api/, an OutOfMemoryError is:

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.

So it happens as soon as the JVM sees that it does not have enough heapspace to allocate memory for the new object you're trying to create. So the object never gets created because you don't have enough memory.

Upvotes: 0

Jack
Jack

Reputation: 133557

From documentation:

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.

Upvotes: 0

Sarel Botha
Sarel Botha

Reputation: 12700

This is thrown when a new object could not be created. The current object will continue to exist.

However, due to the nature of an error like this the current code will stop executing and it's likely that the current object will be garbage-collected soon. It just depends on the structure of your code and whether references are still being held to your object.

Upvotes: 1

Related Questions