Garbage Collection Types in JAVA.
Read this article fisrt :- https://gawesh-prabhashwara.medium.com/constructors-and-garbage-collection-e6308a599dfc
Garbage collections in java,is a mechanism that provides automatic memory management. It is done by the JVM. It need not to handle object allocation and deallocation by the programmer.
Types of Garbage Collectors,
There are types of the garbage collector in java that can be used according to the requirement :-
- Serial Garbage Collector.
- Parallel Garbage Collector.
- Concurrent Mark Sweep (CMS) Garbage Collector.
- Garbage First (G1) Garbage Collector.
The performance and working of each garbage collector are completely different. It has its own pros and cons. Java allows us to choose any one garbage collector that is to be used by the JVM. For the selection of GC, we need to pass JVM arguments. It is difficult to select the right garbage collector for the application.
Serial Garbage Collector,
Serial Garbage collector is well-matched for single-threaded environments. It uses the only thread for garbage collection. It works by holding all the threads of an application. It means that threads of the application freeze by the serial garbage collector during the garbage collection process and the process is known as stop the world event. Avoid the use of serial GC in the server environment. We can use it for simple programs. If you want to use the serial garbage collector, execute the -XX:+UseSerialGC JVM argument to activate it.
Parallel Garbage Collector,
Parallel Garbage Collector is the default GC used by the JVM. The working of the parallel garbage collector is the same as the serial garbage collector. The only difference between serial and parallel garbage collector is that serial garbage collector uses a single thread for garbage collection process while the parallel garbage collector uses multiple threads for the garbage collection. Parallel GC can use multiple CPUs to speed up the application throughput. So, it is also known as throughput collector. It is used if we want to execute a long process (like batch processing) and where long pauses are acceptable. If you want to use the parallel garbage collector, execute the -XX:+UseParallelGC JVM argument to activate it.
We can also use the following JVM arguments in parallel GC :-
*Pause Time: The gap between two GC.
*Throughput Target: The time spent during the garbage collection versus the time spent outside of garbage collection is called throughput target.
Concurrent Mark and Sweep (CMS) Garbage Collector,
CMS uses multiple threads that scan the heap and during the scanning, it marks the instances for eviction, after scanning, it sweeps the marked instances. It does not freeze the application’s threads during the garbage collection. GC threads concurrently execute with the application’s threads. For this reason, it uses more CPU in comparison to other GC. It is also known as the concurrent low pause collector. It also freezes all the threads of the application only if it satisfies the following two scenarios :-
- while marking the referenced objects in the tenured generation region.
- if any change is made to the heap memory in parallel during the garbage collection process.
We can use multiple CPUs for better application throughput. We should use a CMS garbage collector if we have more CPUs for use. Hence, it has an advantage over the parallel garbage collector. If you want to use a CMS garbage collector, execute the -XX:+USeParNewGC JVM argument to activate it. We can also set the number of GC threads by using the -XX:ParallelCMSThreads=<n> JVM argument.
Note: The JVM argument -XX:+UseConcMarkSweepGC has been deprecated because a warning message is issued when it is requested on the command line.
Garbage First (G1) Garbage Collector,
The Eden, survivors, and old areas use this equal-sized region for the memory allocation of the objects. Apart from these memory regions, there are two more types of regions presented in the G1 GC :-
- Humongous: It is used if the object sized is large.
- Available: It represents the unoccupied space.
G1 GC shows a concurrent global marking phase to determine the live and dead objects throughout the heap. After the completion of the mark phase, G1 collects the information of regions that contains the most garbage objects. After that these regions are swept first. If you want to use the G1 garbage collector, execute the -XX:+UseG1GC JVM argument to activate it.
Stop the World Event.
It is a situation when the garbage collector performs the garbage collection (GC) and stops all the application’s threads until the GC process is not completed. The process is known as Stop the World (STW) events.
Improvement in Garbage Collector Since Java 8.
In Java 8, the G1 garbage collector has been updated. The updated GC provides the -XX:+UseStringDeduplication JVM argument that optimizes the heap memory. It removes the duplicate String values to a single char[] array.
JVM Arguments.
The table describes the arguments that can be used to instruct the JVM.
GC Optimization Options.
Usage of JVM GC Options.
We have discussed types of Java garbage collectors. But it is difficult to select one because it depends on the application scenario, availability of hardware, and throughput requirements.
Read this article :- https://gawesh-prabhashwara.medium.com/constructors-and-garbage-collection-e6308a599dfc
Reference :- https://www.javatpoint.com/types-of-garbage-collector-in-java
Article by :- Gawesh.
E-Mail :- gawesh2020java@gmail.com
Blog :- https://gaweshprabhashwara.blogspot.com/
Youtube :- https://www.youtube.com/channel/UCwm7djDtBaueTDqXt_GIFKw
Linkedin :- https://lk.linkedin.com/in/gawesh-prabhashwara-792ab1205
Facebook :- https://www.facebook.com/gawesh98
Twitter :- https://twitter.com/gawesh_98
Instagram :- https://www.instagram.com/gawezh/
Tiktok :- https://www.tiktok.com/@gawesh_prabhashwara?lang=en
0 Comments