I am trying to understand minor, major, and full GC in java. As per my understanding, a threshold is set for young generation objects and when that age is met, the object gets moved to the old generation. This means all objects in the old generation are referenced objects. When there is no space left in the old generation are all the referenced objects removed? If so, what happens to the running application? Will it be stopped?
-
It wouldn't be much of a "full" GC if a major section of the heap was ignored. There are multiple GC implementations - which one do you want to know about? – Michael Feb 08 '21 at 12:50
-
1"*This means all objects in the old generation are referenced objects.*" Not sure I understand the logic of this claim. It seems like you are saying they were referenced for a long time -> they are guaranteed to be referenced forever. Not true – Michael Feb 08 '21 at 12:53
-
By definition only objects that are "eligible for GC" can be removed by GC. That means that there aren't any (strong) references to them anymore. – Hulk Feb 08 '21 at 12:54
-
3"*When there is no space left in the old generation are all the referenced objects removed?*" A GC implementation, unless it is fundamentally broken, does not remove anything that is actively (strongly) referenced. If your heap is full and you are continuing to allocate objects, the GC does not blindly throw away stuff that's still referenced. It would be a recipe for disaster. It will throw an exception. – Michael Feb 08 '21 at 12:55
-
That said, if you are interested in this topic, there are some borderline-cases where optimization can lead to unexpected results, see eg.: https://stackoverflow.com/questions/51636212/is-gc-smart-enough-to-remove-objects-that-are-referenced-but-no-longer-used and https://stackoverflow.com/questions/26642153/finalize-called-on-strongly-reachable-objects-in-java-8 Perhaps the package docs of [`java.lang.ref`](https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/ref/package-summary.html) are also interesting. – Hulk Feb 08 '21 at 13:04
-
2When a new allocation is attempted and there is no space left, an `OutOfMemoryError` is thrown. It’s as simple as that. – Holger Feb 08 '21 at 14:02
1 Answers
First of all, major
and full
is the same thing; people just use different notions to express the same concepts.
Yes, there is a threshold for which objects survive and then are moved to the old regions. As to:
When there is no space left in the old generation are all the referenced objects removed?
When there is no space in old regions, the application is stopped, yes; this is called a stop-the-world
event, since every thread is stopped. GC
will try to move things around to make space at this point in time.
If there is not enough space left, even after Full GC
happens - your application is going to die with an OutOfMemory
( unless you catch it, which is not recommended )
A garbage collector is not going to reclaim live instances. Something that is reachable is not going to disappear just so that you have space; if that would be the case, absolutely no one on this planet would have used java
(any other programming language that has automatic garbage collection).

- 117,005
- 15
- 201
- 306
-
1The application is not going to *die* with an `OutOfMemory` error. The error is thrown like any other throwable and allows to recover (though not recommended). Only when the delivery itself fails, the JVM will forcibly shut down, otherwise, you have to implement the exit yourself. – Holger Feb 09 '21 at 12:33
-
1Mind that when you don’t catch it, it still only terminates the thread that attempted the allocation. Only when all non-Daemon threads terminated for that reason or another, the OOME has the effect of killing the application. Further, since futures usually catch all throwable, you have to react on it yourself, i.e. log and exit… – Holger Feb 09 '21 at 15:29