Swerrgy Smith wrote:
I don't understand the real benefit of a generational garbage collector. As I understand, the generational garbage collector divides heap memory in different region and the young generation region (Eden region) is visited more frequently than other region. The purpose is to REDUCE THE TIME NEEDED to collect the unused objects because most of the objects have a short life.
However, the GC still have to traverse ALL THE OBJECT TREE from the root set and visit all the objects, even if these object are in older region (tenured region). How can the time be reduced compared to a garbage collector which does NOT use generational model? Because in a non-generational GC, the GC also has to traverse ALL the object tree from the root set.
It has been a while... so hopefully, I remembered everything correctly.
The new generation collector is a copy collector. How it works is to treat the heap as two halves. During collection, anything that is reachable is moved from the current half to the other half (and all the references in the JVM has to be fixed). This means that it is really expensive when an object is reachable. On the other hand, when an object is not reachable, it is really easy to deal with -- just ignore it, as it will be wipe because it is in the half that will be reused.
Hence, it is very good for the new generation, as most newly created objects are very short lived; but it is bad for the tenure generation, as it will be constantly moving the objects between the two halves. And moving an object is much much more expensive than marking an object as reachable, as the GC has to find all references and fix it.
Also, the parallel version of the new generation collector is ... well... parallel. This mean that it will use all the cores in the machine, making it really fast at determining reachability.
The tenure generation collector is a mark and sweep collector. It is not as fast as the new generation collector, but it uses the heap more efficiently -- as it just compacts the heap. It is also not as efficient for two other reasons; it has to deal with the majority of objects which are reachable (not collected), and it is not parallel. However, it is concurrent -- meaning that the program does not have to stop while it is collecting. This means that the GC can take it time, with not too much of an impact on the application -- to probably collect a smaller amount than the new generation would collected.
Also, two more reasons why this collector is good for the tenure generation... first, since it is a compacting collector, objects slowly move towards the beginning, until it is an area where everything is always reachable, hence, not being collected, hence, not being moved. Second, this collector is less efficient when an object is not reachable. Not reachable objects creates a hole, and holes needed to be close via compaction, which means objects needs to be moved.
Henry