how the live objects are figured out in young generation collection ?
A good high-level description of how generational collection is implemented in HotSpot may be found in this article.
In general a generational collector marks the young generation as follows:
- It marks young objects and traces references starting with the thread stack frames and the static frames. When it finds a reference to an old space object it ignores it.
- Then it repeats the process for references in the old space that refer to young space objects. Identifying these references in the old space is the tricky bit.
In HotSpot, old space objects that contain young space references are identified using the "Card Table". The old space is divided into regions of 512 bytes, and each region has a "Card". If the region contains any old -> new space pointers, a bit in the Card is set.
Objects in regions with the Card bit set are then traced during a new space collection.
The tricky thing is maintaining the Card table as new space references are written to old-space objects. In HotSpot, this is implemented using a software write-barrier that sets the appropriate Card's dirty bit whenever a new space reference is written into the memory region corresponding to the Card. As the linked article notes, this makes setting a reference field in an object more expensive, but it is apparently worth it due to the time saved by being able to collect only the new space most of the time.