Next: Optimal Throughput GC policy
Garbage collector
The garbage collector...
- Keeps track of which objects are still referenced
- Removes all other objects, which are considered "garbage"
Garbage collection only occurs when...
- An allocation failure occurs due to lack of room in the heap
- When a call to System.gc() occurs
When an allocation failure occurs, the running thread that had the failure takes exclusive control of the VM. All application threads stop and garbage collection commences. This is referred to as a stop-the-world event.
The process of garbage collection consists of three phases:
Mark phase all reachable objects are identified and all else is considered garbage. Sweep phase space used by all unmarked objects is reclaimed. Compaction phase objects are moved into as contiguous a space as possible and references are updated. The compaction phase is the most costly because objects are moved and therefore require all references to them to be updated as well. Thus, compaction is always avoided if possible.
See also