Why is heap divided into Eden, Survivor spaces and Old Generation?

前端 未结 2 1516
醉梦人生
醉梦人生 2020-12-18 04:43

Could you please answer me a question about JVM Garbage Collection process?

Why is heap divided into Eden, Survivor spaces and Old Generation?

When a young e

相关标签:
2条回答
  • 2020-12-18 05:22

    The division is very useful if you consider moving collectors. If there were no separation a young collection would leave a lot of holes in the heap, requiring either free list management or compaction of the old gen.

    If on the other hand the young generation is implemented as a semi-space GC no such cleanup and tracking is required because the evacuated space will by definition only contain dead objects after a minor collection and can thus be considered free space afterwards. This also enables bump pointer allocation in the young gen.

    0 讨论(0)
  • 2020-12-18 05:24

    The basic premise is that when new objects are created, no reference from an old object to the new one exists and for a lot of objects, or even most of them, this never changes. This implies that you can do a “minor” garbage collection scanning the young generation only, if you can prove that there are still no references from old objects to new objects or when you know precisely which references have been created.

    This implies that reference changes to old objects must be tracked and remembered (but recall the premise that such changes don’t happen so often).

    One implementation strategy is Card Marking:

    If a garbage collector does not collect the entire heap (an incremental collection), the garbage collector needs to know where there are pointers from the uncollected part of the heap into the part of the heap that is being collected. This is typically for a generational garbage collector in which the uncollected part of the heap is usually the old generation, and the collected part of the heap is the young generation. The data structure for keeping this information (old generation pointers to young generation objects), is a remembered set. A card table is a particular type of remembered set. Java HotSpot VM uses an array of bytes as a card table. Each byte is referred to as a card. A card corresponds to a range of addresses in the heap. Dirtying a card means changing the value of the byte to a dirty value; a dirty value might contain a new pointer from the old generation to the young generation in the address range covered by the card.

    Processing a card means looking at the card to see if there is an old generation to young generation pointer and perhaps doing something with that information such as transferring it to another data structure.

    Of course, using generations only provides a benefit, if it enables us to skip certain memory regions during the scan and if maintaining these remembered sets does not outweigh the savings.

    0 讨论(0)
提交回复
热议问题