how the live objects are figured out in young generation collection?

前端 未结 3 1854
广开言路
广开言路 2021-02-06 04:54

I understand that time taken by YGC is proportional to number of live objects in Eden. I also understand that how the live objects are figured out in Major collections (All the

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-06 05:33

    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 (assuming we just have two generations):

    1. 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 generation object it ignores it.
    2. Then it repeats the process for references in the old generation that refer to young generation objects. The tricky bit is identifying these references in the old generation without marking the entire old generation.
    3. Now we have marked all of the objects in the new generation that are reachable ... and the rest (in that generation) can be reclaimed.

    In HotSpot, old generation objects that contain young generation references are identified using the "Card Table". The old generation is divided into regions of 512 bytes, and each region has a "Card". If the region contains any old -> new generation pointers, a bit in the Card is set. Objects in regions with the Card bit set are then traced during a new generation collection.

    The tricky thing is maintaining the Card table as new space references are written to old generation 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 generation most of the time.

提交回复
热议问题