Why garbage collection in young generation is fast depends on a fact: most objects in young generation die soon. So when a young generation collection happens, few objects which are still alive are moved into old generation, and since then, all the data in young generation can be thought as useless(part or them is moved, the reset is unreachable), and this peace of memory can be re-used without further scanning. In this case, unreachable objects are not being scanned, which save a lot of time.
But I got a question, in Java there's a method finalize()
(though it is deprecated in JAVA 9), if a collector guarantees that finalize()
will get called, it needs to scan unreachable objects, not just go though the living objects, and the speed advantage seems disappeared.
So,
- I'm I right? (
finalize()
makes young generation collections much slower and the speed advantage disappeared.) - If not, how JVM handled this problem? (For example, ignore
finalize()
?) - Besides of the problem above, do young generation has other advantage?
Edit:
I'm writing a gc for a language with finalize()
feature, I just can't make the collection fast in this situation.