I'm trying to understand garbage collection mechanisms and I'm on generational algorithms and I have a question about young/old generation difference. I read that to start collecting objects in young generation GC is marking them starting from GC roots to find the live ones and usually it copies them to survivor space, clears young generation area and voila.
I don't get though if we start in GC roots and we start to go through live objects don't we find also object in old generation? Does it mean that when we hit an object in an old space we stop tracking references at that point or what?
Although I have not directly checked this, common sense dictates that the object graph traversal is curtailed each time an object from the Old Generation is encountered. Note that this check is very cheap to make: a simple range check on the pointer value suffices to determine object's placement within a heap region.
There is another vital point to consider, however: what if a young object is reachable only through an old object? Clearly, the Old Generation must be considered somehow.
Enter the card table: this is a backing structure in front of each heap region where a compressed "bitmap" view of the region is held, such that each bit corresponds to, say, 256 bytes of heap. Each time a reference-typed variable is updated, the corresponding bit in the card table is raised to 1, meaning "dirty".
With the card table in place, the following happens on each YG collection: all the chunks of heap marked as "dirty" are scanned for pointers to objects within the Young Generation. Each object found this way is considered reachable.
A corrolary of the above: young objects reachable through an old object which has in the meantime become garbage will be considered reachable and pollute the heap until a Major GC takes place.
When the GC run in Young Generation, it's called a Minor Collection. Objects in Old Generation are not concerned by this type of collection.
And yes objects directly reachable from the roots are marked as alive but they can be elsewhere in the heap so they can be objects in Old Generation.
Even if Old Gen object are reachable, minor collections won't reclaim garbage in Old Gen.
Quoting the HotSpot doc :
When the young generation fills up it causes a minor collection in which only the young generation is collected; garbage in other generations is not reclaimed
For me it means that GC will go through the object graph, and maybe find garbage in OldGen but won't reclaim for it.
I've found this IBM article which explain quite well how intergenerational references tracking works in HotSpot GC.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With