Types of Garbage Collector in Java
Garbage collection is a Java feature that offers automatic memory management. The JVM is in charge of it. The programmer does not have to handle object creation and deallocation. We have previously covered the operation of rubbish collection.
We advise going over the Java heap, garbage collection, and memory management first if you haven't been introduced to them. We will talk about the different kinds of garbage collection using Java in this section.
Garbage Collector Types
In Java, there are four different types of garbage collectors that can be employed depending on the situation:
- Serial Garbage Collector
- Garbage First (G1) Garbage Collector
- Concurrent Mark Sweep (CMS) Garbage Collector
- Parallel Garbage Collector
Each garbage collector operates and performs in a completely different way. Each pro and con is distinct. Java gives us the option to select which garbage collector the JVM will employ. We must pass JVM arguments in order to choose the GC. Choosing the appropriate garbage collector for an application is challenging. Let's talk about each waste collector individually.
Serial Garbage Collector
The single-threaded environment is a good fit for the serial garbage collector. It performs garbage collection using the only thread. It functions by keeping track of every thread in a program. The term "stop the world event" refers to a procedure when application threads are frozen either by serial garbage collector even during garbage collection operation. Serious GC should not be used in a server setting. It can be used for straightforward programmes. Run the -XX:+UseSerialGC JVM parameter to enable the serial garbage collector if you want to utilise it.
Parallel Garbage Collector
The JVM uses Parallel Garbage Collector as its default garbage collector. A parallel garbage collector operates in a manner that is identical with that of the serial garbage collector. The sole distinction among serial communication garbage collectors is that although parallel garbage collectors use many threads for garbage collection, serial garbage collectors only use one thread. Multiple CPUs can be used in parallel GC to increase application throughput. It also goes by the name throughput collector. It is utilised for running lengthy processes when extended pauses are acceptable, such as batch processing. Use the -XX:+UseParallelGC JVM parameter to enable a parallel garbage collector if you want to use it.
Concurrent Mark and Sweep (CMS) Garbage Collector
Multiple threads are used by CMS to scan the heap, mark instances for eviction throughout the scan, and then sweep the tagged instances. The threads of the programme are not frozen while the garbage is being collected. GC threads run in parallel with application threads. Because of this, it utilises more CPU than other GC. The continuous low pause collector is another name for it. Additionally, it only freezes the application's threads if the following two conditions are met:
- as in tenured generation region, while marking the referred items.
- if any parallel changes are made to the heap memory while the garbage collection procedure is running.
For increased application throughput, we can leverage additional CPUs. If there are additional CPUs available, we ought to employ a CMS trash collector. In comparison to the parallel garbage collector, it is advantageous. Utilize the -XX:+USeParNewGC JVM parameter to activate the CMS garbage collector if you want to use it. The JVM parameter -XX:ParallelCMSThreads=n> can be used to set the amount of GC threads as well.
Garbage First (G1) Garbage Collector
If our memory is huge (greater than 4GB), the G1 garbage collectors is used (heap space). It prioritises the equal-sized (often 1MB to 32MB) chunks of the heap before performing concurrent garbage collection on those chunks in accordance with the priority.
This equally sized sector is used for the system memory of the objects in the Eden, surviving, and old areas. In addition to such memory regions, the G1 GC also has the following two additional sorts of regions:
Humongous: This adjective is used to describe enormous objects.
Available: It stands for the empty space.
The concurrent global marking step for identifying living and dead objects all through the heap is shown in G1 GC. Following the marking phase, G1 gathers data on the areas with the highest concentration of rubbish objects. These areas are then washed first after that. Use the -XX:+UseG1GC JVM parameter to activate your G1 garbage collector if you want to use it.