user5536368
user5536368

Reputation:

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

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?

Upvotes: 7

Views: 3475

Answers (2)

the8472
the8472

Reputation: 43170

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.

Upvotes: 1

Holger
Holger

Reputation: 298599

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.

Upvotes: 3

Related Questions