The eden This is where most new objects are allocated (not all, as large objects may be allocated directly into the old generation). The eden is almost always empty after a minor garbage collection.
The two survivor spaces (S1, S2) These hold objects that have survived at least one minor garbage collection.
The Old generation Objects that are longer-lived are eventually promoted, or tenured, to the old generation.
The Application is not running.
Eden Allocation
Allocations into the eden by using what’s referred to as "the bump-the-pointer technique"
The end of the last allocated object is tracked, and when a new allocation request needs to be satisfied, the allocator needs only to check whether it will fit between top and the end of the eden. If it does, top is bumped to the end of the newly allocated object
The Application is running.
Minor Garbage Collection 01
Objects that have been found to be garbage are marked with a X.
Live objects in the eden that survive the collection are copied to the unused survivor space(S1).
Finally the eden is entirely empty
The Application is stopped for garbage collection.
Minor Garbage Collection 02
Objects that have been found in Eden and Survivor(S1) to be garbage are marked with a X.
Live objects in the eden that survive the collection are copied to the unused survivor space(S2).
Live objects in the survivor space that is in use, which will be given another chance to be reclaimed in the young generation, are also copied to the unused survivor space(S2).
At the end of the minor garbage collection, the two survivor spaces swap roles.
Finally the eden is entirely empty and only one survivor space is in use.
The Application is stopped for garbage collection.
Minor Garbage Collection 03
Same as before. just for checking swapped survivor spaces.
The Application is stopped for garbage collection.
Minor Garbage Collection 04
Live objects in the survivor space that is in use, that are deemed “old enough”, are promoted to the old generation.
During a minor garbage collection, there is no guarantee that the allocating survivor space will always be large enough to accommodate the surviving objects from both the eden and the other survivor space. If it overflows, the rest of the objects that need to be evacuated will be moved to the old generation. This is referred to as "premature promotion".
The Application is stopped for garbage collection.
Full Garbage Collection
mark-compact garbage collector
The mark-compact garbage collector first identifies which objects are still live in the old generation.
Then the collector slides live objects toward the beginning of the heap, leaving any free space in a single contiguous chunk at the end of the heap.
Future allocations into the old generation to use the fast bump-the-pointer technique.
The Application is stopped for garbage collection.
Minor Garbage Collection 05
This is basic cycle of Serial Garbage collection.(same as Parallel Garbage collection but multithreaded collectors)
The Application is stopped for garbage collection.