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

2

7

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 evacuation is processed objects are visited through references starting from the roots to find out unreachable ones. Reachable objects are marked as ‘alive’ and unreachable are not marked and will be eliminated.

As a result, ALL objects are considered, including objects allocated in Old Generation are also visited and marked if they are reachable.

As I understand reclaiming Young Generation and Old Generation at once is demanding because these generations are located in different contiguous parts of memory.

But why do we need this division if even after the simplest marking on the Young evacuation level we have the entire bitmap with all alive and dead objects if all reachable and unreachable objects are known and can be deleted?

I also know weak generational hypothesis about but why do we need the division?

Kamp answered 27/11, 2017 at 4:52 Comment(0)
F
3

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.

Forbear answered 27/11, 2017 at 7:44 Comment(5)
Thank you for your answer! Could you please explain in details how 'scanning the young generation only' works?)Kamp
@PavelPavel that means scanning only a portion of the heap - the much smaller part of the heap, since it's small it requires very little time to scan and re-claim memoryAbolition
I suppose, just starting with certain gc roots only (scan local variables, but not static fields) and not traversing any reference pointing to the old generation.Forbear
@Forbear thank you! I can't find any step-by-step tutorial about scanning in minor collectionKamp
That’s not surprising. Most online resource attempt to provide an overall picture only, not a “step-by-step tutorial”, as you are not supposed to be a garbage collector, not even to implement a garbage collector yourself… By the way, your question was also my first question when I heard about “minor collections” the first time. Given that the garbage collector tracks live references rather than garbage, it sounds contradicting. It only makes sense after knowing about the remembered sets and that altering old objects becomes a monitored action in such a setup.Forbear
R
1

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.

Rickey answered 27/11, 2017 at 18:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.