Garbage collection is a way of collecting and discarding stale objects in JVM and releasing the memory so that it can be reused to store live objects.
- All Garbage collection implementations has mainly three passes
- MARK: Garbage collector marks the stale objects
- SWEEP: It then reclaims the memory allocated to the objects marked in the first step
- COMPACT: In this phase garbage collector compacts all the used space so as to combine all the free memory thus reducing memory fragmentation.
- At any point in time there are two types of threads which are running in JVM
- Application threads
- GC threads
- STOP THE WORLD PAUSE: It is possible that during GC JVM pauses all the application threads. These pauses should be minimised and are known as “Stop the world pauses”.
- MINOR GC: When the young generation fills up the GC stop the application threads and empty out the young generation. This is known as minor GC. All GC algorithms have stop the world pauses while clearing out the young generation. Because of the heap design the young generation is only a portion of the complete heap and so these pauses are very small but they can occur frequently as young is smaller.
- MAJOR GC: These pauses are also there when the old generation is garbage collected but in the case of old generation they can be more lengthy.
Types of Garbage collection implementations
Serial GC (-XX:+UseSerialGC):
- Serial GC uses the simple mark-sweep-compact approach for young and old generations garbage collection i.e Minor and Major GC.
- It uses a single thread and will stop all application thread both for minor or major GC. It is generally used for single core client class machines.
Parallel/Throughput GC (-XX:+UseParallelGC):
- Parallel GC is same as Serial GC except that is spawns N threads for young generation garbage collection where N is the number of CPU cores in the system.
- It is still single threaded for Old Generation.
- It again stops all the application threads for both minor and major GC. This is the default GC algorithm in server class multi-core machines.
Parallel Old GC (-XX:+UseParallelOldGC):
- This is same as Parallel GC except that it uses multiple threads for both Young Generation and Old Generation garbage collection.
Concurrent Mark Sweep (CMS) Collector (-XX:+UseConcMarkSweepGC):
- The main intention of CMS is to make the pauses smaller both in case of old generation and young gen.
- However this comes with added CPU usage as CMS runs background threads to mark the unused objects in old generation.
- For new generation CMS also stops all application threads.
- These background threads do not do any compaction and so heap can become fragmented.
- If heap becomes too fragmented CMS switches to Serial GC to compact the old generation also stopping application threads in the process.
G1 Garbage Collector (-XX:+UseG1GC):
- The Garbage First or G1 garbage collector is available from Java 7 and it’s long term goal is to replace the CMS collector.
- The G1 collector is a parallel, concurrent, and incrementally compacting low-pause garbage collector by again using background threads to accomplish most of the work.
- In this it does not see the JVM as new and old but divides the complete JVM heap into multiple smaller size compartments causing less fragmentation and smaller stops.
- Also G1 compacts the heap as it goes. G1 also has less chances of seeing a full GC than CMS. On heap larger than 4 GB G1 outshines CMS.