In this tutorial we will learn about several existing garbage collectors. In Java, garbage collection is an automatic process that can replace programmers with complex tasks such as memory allocation and recycling. This article is the third article in the garbage collection tutorial series. In the previous part 2, we saw how garbage collection works in Java. That is an interesting article. I recommend you to take a look. The first part introduces Java garbage collection, mainly including JVM architecture, heap memory model and some Java terms.
Java has four types of garbage collectors:
Serial Garbage Collector
Parallel Garbage Collector
Concurrent tag scanning garbage collector (CMS Garbage Collector)
G1 Garbage Collector
Each type has its own strengths and weaknesses. Importantly, when we program, we can select the garbage collector type through the JVM. We select by passing parameters to the JVM. Each type is very different and can provide us with completely different application performance. It is very important to understand each type of garbage collector and make the right choice based on application selection.
1. Serial garbage collector
The serial garbage collector works by holding all threads of the application. It is designed for a single thread environment and uses only a single thread for garbage collection, and works by freezing all application threads, so it may not be suitable for server environments. It is best suited to simple command line programs.
The serial garbage collector can be used through the JVM parameter -XX:+UseSerialGC.
2. Parallel garbage collector
Parallel garbage collector is also called throughputcollector. It is the default garbage collector for JVM. Unlike serial garbage collectors, it uses multithreading for garbage collection. Similarly, it also freezes all application threads when performing garbage collection
3. Concurrent mark scanning garbage collector
Concurrent tag garbage collection uses multithreaded scanning of heap memory, marking instances that need to be cleaned and cleaning up marked instances. The concurrent tag garbage collector will only hold all threads of the application in the following two cases.
When the tagged reference object is in the tenured area;
During garbage collection, the data in the heap memory is changed concurrently.
Concurrent tag scanning garbage collector uses more CPU to ensure program throughput than parallel garbage collectors. If we can allocate more CPU for better program performance, then concurrent markup on the scan garbage collector is a better option than the concurrent garbage collector.
Open the concurrent mark scan garbage collector via JVM parameter XX: + USeParNewGC.
4. G1 garbage collector
The G1 garbage collector is suitable for situations where heap memory is large. It divides the heap memory into different areas and garbage collects it concurrently. G1 can also compress the remaining heap memory space after reclaiming memory. Concurrent scan marks the garbage collector compresses memory in the STW case. G1 garbage collection will give priority to the area with the most garbage in the first piece.
Use G1 garbage collector via JVM parameter XX:+UseG1GC
New features of Java8
When using the G1 garbage collector, use the JVM parameter -XX:+UseStringDedupplication. We can optimize the heap memory by removing duplicate strings and keeping only one char[]. This option was introduced in Java8u20.
We have given all four Java garbage collectors, which one needs to be used based on application scenarios, hardware performance and throughput requirements.
JVM configuration for garbage collection
The following JVM key configurations are all related to Java garbage collection.
Type of garbage collector running
| Configuration | describe |
|---|---|
| -XX:+UseSerialGC | Serial garbage collector |
| -XX:+UseParallelGC | Parallel garbage collector |
| -XX:+UseConcMarkSweepGC | Concurrent tag scanning garbage collector |
| -XX:ParallelCMSThreads= | Concurrent tag scan garbage collector = number of threads used |
| -XX:+UseG1GC | G1 garbage collector |
Optimized configuration of GC
| Configuration | describe |
|---|---|
| -Xms | Initialize the heap memory size |
| -Xmx | Maximum heap memory value |
| -Xmn | Chronicle size |
| -XX:PermSize | Initialize the permanent generation size |
| -XX:MaxPermSize | Permanent generation maximum capacity |
Example of using JVM GC parameters
java -Xmx12m -Xms3m -Xmn1m -XX:PermSize=20m -XX:MaxPermSize=20m -XX:+UseSerialGC -jar java-application.jar
Summarize
The above is all about a brief understanding of the types of Java garbage collectors in this article, and I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!