Garbage Collectors
Garbage Collecting is an automatic process that detects unused objects in heap and automatically removes them.
Common Concepts
Young Generation - new objects, just recently allocated.
Old Generation - old, long-lived objects, that survived minor garbage collection.
Minor or Incremental Garbage Collection - process of cleaning unreachable objects from young generation.
Major or Full Garbage Collection - process of removing unreachable objects from the old generation. Happens less frequent than minor garbage collection.
Unreachable Object - object which doesn't have any reference to pointing it.
Basically speaking, GC works in 2 steps - Mark and Sweep
Mark - stage where GC marks which objects in heap are used and which are not (unreachable).
Sweep - removal of objects marked as unused on the previous step.
GC Implementations
Serial Garbage Collector
The simplest implementation, works in a single thread. When it works, this GC causes the Stop the World pauses, those this GC is not ideal variant when it would cause a problem. Although, it can be a good choice on single core machines.
SerialGC splits heap into two main blocks: Young Generation and Old Generation. Young Generation contains Eden - block with newly created objects, and multiple Young Generation blocks.
When an object in Young Generation survives several garbage collections—it is being promoted into Old Generation.
This whole process needs to stop the whole application so that references are not broken, thus requires Stop the World.
Understanding JVM Garbage Collection: Serial and Parallel Collectors @ Medium
Parallel Garbage Collector
Also known as throughput collector. Similar to Serial GC, but performs its work in parallel by multiple threads.
Concurrent Mark-Sweep (CMS) Collector
G1 Garbage Collector
A Mostly Concurrent collector.
The heap is split into multiple sections - Eden, Survivor and Old generation. Each block has size from 1MB to 32MB. Total number of blocks is up to 2048.