Please visit the below pages before going further in the current topic for better understanding of Garbage Collection.
JVM provides four options for us to select the apt Garbage Collector for our Application.
- Serial Garbage Collector
- Parallel Garbage Collector
- CMS Garbage Collector
- G1 Garbage Collector
Let’s discuss the utility of each of the above GC types one by one.
Serial Garbage Collector
- This is simplest available GC. It uses a single thread for Garbage Collection.
- As a result, this freezes all application threads when it runs.
- Hence, it is not a good idea to use it in multi-threaded applications.
- Add -XX:+UseSerialGC in JVM args to set this GC explicitly.
Parallel Garbage Collector
- It uses multiple threads to scan through the heap space for Garbage Collection.
- It also pauses the application threads while performing minor or full GC.
- It is best suited if applications that can handle such pauses, and try to optimize CPU overhead caused by the collector.
- Also known as Throughput collector.
- Add -XX:+UseParallelGC in JVM args to set this GC explicitly.
- Parallel GC is the default garbage collector in JDK 8.
Concurrent Mark Sweep (CMS) Garbage Collector
- It uses multiple threads to scan the heap memory consistently to the mark objects that are unused and then sweep the marked objects.
- CMS garbage collector holds all the application threads in the following two scenarios only,
- while marking the referenced objects in the tenured generation space.
- if there is a change in heap memory in parallel while doing the garbage collection.
- If we compare CMS collector with Parallel garbage collector, CMS collector uses more CPU to ensure better application throughput.
- If we are developing an application where we can provide more CPU resources for better performance then CMS garbage collector is the preferred choice over the parallel collector.
- Add –XX:+USeParNewGC in JVM args to set this GC explicitly.
Garbage-First (G1) Garbage Collector
- G1 (Garbage First) Garbage Collector is designed for applications running on multi-processor machines with large memory space. Basically it is mainly designed for an application having heap size greater than 4GB.
- Unlike other collectors, G1 collector partitions the heap into a set of equal-sized heap regions, each a contiguous range of virtual memory. When performing garbage collections, G1 shows a concurrent global marking phase to determine the liveness of objects throughout the heap. After the mark phase is completed, G1 preferentially collects regions with the least amount of live data, or “garbage first”. It collects in these areas first, which usually yields a significant amount of free space. Hence the name: Garbage-First(G1) GC.
- It’s available since JDK7 Update 4 and in later releases.
- G1 Garbage Collector is the default garbage collection of Java 9.
- The biggest advantage of the G1 GC is its performance. It is faster than the other 3 GC types.
- Add -XX:+UseG1GC in JVM args to set this GC explicitly.