Type of Garbage Collector

Before discussing the type of garbage collector, it is important to understand some basic terms related to it.

  • Stop the World Event: It is a time when all the application threads are stopped until the garbage collection operation completes. Both minor and major garbage collections are “Stop the World” events.
  • -Xms: This command-line argument specifies the initial heap size for JVM. It sets the initial heap size for when the JVM starts.
  • -Xmx: It sets the maximum heap size.
  • -Xmn: It sets the size of the Young Generation.
  • -XX:PermSize: It sets the starting size of the Permanent Generation.
  • -XX:MaxPermSize: It sets the maximum size of the Permanent Generation

Type of Garbage Collector:

1. Serial GC:

Serial GC is designed for single-threaded environments. It uses just a single thread for garbage collection. As a result, this GC implementation freezes all application threads when it runs. This leads to stop the world event. Hence, it is not a good idea to use it in multithreaded applications like server environments. JVM argument to use the serial garbage collector is:


Use: Serial GC is a good option when you do not have low pause time requirements and GC runs on client-style machines. 

GC Serial Type
Figure 01: Serial GC

2. Parallel GC: 

This is also called ‘Throughput Collectors’. Since it can use multiple CPUs to speed up application throughput. This collector should be used when a lot of work needs to be done and long pauses are acceptable. For example, batch processing like printing reports or bills or performing a large number of database queries. It is specially designed for multi-threaded environments. It uses multiple parallel threads for garbage collection. This GC implementation also freezes all application threads when it runs and leads to a ‘stop the world event’. JVM argument to use the parallel garbage collector is:


You can also specify maximum garbage collection threads and pause time and throughput in parallel GC using the following arguments.

a. To control the number of garbage collector threads 


where n = no. of GC threads

b. To specify the maximum pause time (a gap between two GC) 


where n = time in milliseconds

c. To specify the maximum throughput target (measured regarding the time spent doing garbage collection versus the time spent outside of garbage collection) 


where n = GC time vs outside GC time

Use: Parallel GC is helpful where a lot of work needs to be done and long pauses are acceptable.

GC Parallel Type
Figure 02: Parallel GC

3. Concurrent Mark Sweep (CMS) GC: 

The Concurrent Mark Sweep (CMS) garbage collector collects the tenured generation.  It is also called as the concurrent low pause collector. It attempts to minimize the pauses due to garbage collection by doing most of the garbage collection work concurrently with the application threads. Due to this reason, the CMS collector uses more CPU than other GCs. If you can allocate more CPU for better performance then CMS garbage collector is a preferred choice over the parallel collector. CMS garbage collector uses multiple threads to scan the heap memory to mark instances for eviction and then sweep the marked instances. Stop the world event occurs only when

1. marking the referenced objects in the tenured generation space.
2. if there is a change in heap memory in parallel while doing the garbage collection. 

JVM argument to use the CMS garbage collector is:


To set the number of threads:


where n = no. of GC threads

Use: CMS GC is helpful when a low pause time is needed and resources (memory, CPU) can be shared with the garbage collector

CMS Garbage Collector
Figure 03: CMS Garbage Collector

4. G1 GC:

G1 (Garbage First) Garbage Collector is designed for applications running on multi-processor machines with large memory space (larger than 4GB). G1 collector partitions the heap into a set of equal-sized heap regions (1MB to 32MB – depending on the size of the heap). The Eden, survivors and old area use this equal size region for the memory allocation of the objects.

Apart from Eden, Survivor, and Old (Tenured) memory regions, there are two more types of regions present in the G1 GC method.

  • Humongous
  • Available

The Humongous region is used for large-size objects whereas the Available region shows unused space. While performing garbage collections, G1 shows a concurrent global marking phase to determine the live and dead objects throughout the heap. After the mark phase is completed, G1 knows which regions contain the most garbage objects, so those regions are swept first. It is why this method of garbage collection is called Garbage-First. JVM argument to use the G1 garbage collector is:


Some important points related to G1GC:

  • In G1, consecutive memory spaces are divided into fixed sizes named regions.
  • The basic region target value is divided into 2048 (2K) spaces. If 8G is the maximum size of Java Heap, the size of one region will be 4MB. (8192MB / 2048 = 4MB)
  • The region space can be adjusted from 1MB to 32MB with the -XX: G1RegionSize option, but it is not recommended that you directly tune.
  • GC operation for humongous regions is not optimized. Therefore, if the large object size is a problem in using G1 GC, it is a good idea to analyze the program to see why large objects are a problem. 
  • In Java 8, some improvements are done in the G1 garbage collector. Using -XX:+UseStringDeduplication JVM argument using along with the G1 garbage collector. This helps to remove duplicate string values to a single value in a single char[] array.

Some more important arguments:

  • To control the heap space occupancy when the concurrent cycle starts. The default percentage is 45%: 


where n = % of heap space

  • If a live object in the OLD region exists with a value greater than or equal to this option, it is excluded from the GC object. The default value is 65.


where n = time in milliseconds

  • To specify how many regions are allowed to be wasted. The default value is 10. 


where n = no. of regions

Use: G1 GC provides the benefit where multi-processor machines along with large memory space are available. G1 GC gives better GC performance than others on a large memory system.

GC G1 Type
Figure 04: G1 GC

You may be interested:

Leave a Comment