Garbage Collector (GC)
At its most simple, Garbage Collection includes finding memory that is no longer in use and making it available for reuse.
Java has below four types of garbage collectors:
- Serial Garbage Collector
- Parallel Garbage Collector
- CMS Garbage Collector
- G1 Garbage Collector
Serial Garbage CollectorThe Serial garbage collector works by holding all threads of the application, it uses just a single thread for garbage collection. It's designed for single-threaded environments. While doing garbage collection, the way it works by freezing all the application threads may not be appropriate for a server environment, can be appropriate for simple command-line programs.
Use below option to turn on the Serial Garbage Collector.
Parallel Garbage CollectorIt is the default garbage collector of the JVM, also known as throughput collector. It uses multiple threads for garbage collection as opposed to the serial garbage collector. This also freezes all the application threads when performing garbage collection.
CMS Garbage CollectorConcurrent Mark Sweep (CMS) garbage collector uses multiple threads to scan the heap memory to mark instances for eviction and then sweep the marked instances. CMS garbage collector holds all the application threads, while marking the referenced objects in the tenured generation space and if there is a change in heap memory in parallel while doing the garbage collection.
CMS collector uses more CPUs to ensure better performance of the application. CMS garbage collector compacts the memory on stop the world (STW) situations.
Turn on the CMS Garbage Collector using below option.
G1 Garbage CollectorG1 divides the heap memory into regions and performs parallel collection within them. It is used for large heap memory areas. G1 compacts the unused heap space on the go just after the memory has been reclaimed.
Turn on the G1 Garbage Collector using below option.
Java 8 has optimized the heap memory by removing duplicate String values to a single char array. Use below option in Java 8 to turn on the G1 Garbage Collector.