This article consists ofimportnew - nice translator from Javapapers. Directory
- Introduction to Garbage collection
- How does garbage collection work?
- Categories of garbage collection
- Garbage collection Monitoring and analysis
In this tutorial we will learn several existing garbage collector. In Java, garbage collection is an automated process that can replace programmers in allocating and reclaiming memory for these complex tasks. This is the third of the garbage Collection tutorial series, in the first part of the 2nd we see how garbage collection works in Java, it is an interesting article, I recommend you to look at. The first section introduces Java garbage collection, which includes the JVM architecture, the heap memory model, and some Java terminology.
Java has four types of garbage collector:
- Serial garbage collector (Serial garbage Collector)
- Parallel garbage collector (Parallel garbage Collector)
- Concurrent tag Scan garbage collector (CMS garbage Collector)
- G1 garbage collector (G1 garbage Collector)
Each of these types has its own strengths and weaknesses. The important thing is that we can choose the garbage collector type through the JVM when we program. We select by passing parameters to the JVM. Each type is largely different and can provide us with a completely distinct application performance. It is important to understand each type of garbage collector and make the right choices based on your application choices.
1. Serial garbage collector
The serial garbage collector works by holding all the threads of the application. It is designed for single-threaded environments and uses only a single thread for garbage collection and works by freezing all application threads, so it may not be suitable for a server environment. It is best suited to a simple command-line program.
The -XX:+UseSerialGC
serial garbage collector can be used with JVM parameters.
2. Parallel garbage collector
The parallel garbage collector is also called throughput Collector. It is the default garbage collector for the JVM. Unlike the serial garbage collector, it uses multithreading for garbage collection. Similarly, it freezes all application threads when garbage collection is performed
3. Concurrent tag Scan garbage collector
Concurrent tag garbage collection uses multithreaded scanning of heap memory, marking instances that need to be cleaned up, and cleaning up flagged instances. The Concurrency tag garbage collector only holds all of the application threads in the following two scenarios.
- When the tagged reference object is in the tenured area;
- In the garbage collection, the data of the heap memory is changed concurrently.
Compared to the parallel garbage collector, the concurrent tag scan garbage collector uses more CPUs to ensure program throughput. If we can allocate more CPUs for better program performance, then scanning the garbage collector on a concurrent tag is a better choice than the concurrent garbage collector.
XX:+USeParNewGC
opens the Concurrency tag scan garbage collector with the JVM parameters.
4. G1 garbage collector
The G1 garbage collector is suitable for large heap memory situations where he splits heap memory into different zones and concurrently recycles them. G1 can also compress the remaining heap memory space after the memory is reclaimed. The concurrent scan flag garbage collector compresses memory in the case of STW. G1 garbage collection will prioritize the first block of trash
Using the G1 garbage collector with JVM parameters –XX:+UseG1GC
New features for Java 8
When using the G1 garbage collector, the JVM parameters are passed -XX:+UseStringDeduplication
. We can optimize heap memory by removing duplicate strings and preserving only one char[]. This option is introduced in Java 8 u 20.
We've given all four Java garbage collections, which need to be determined based on the application scenario, hardware performance, and throughput requirements.
JVM Configuration for garbage collection
The following JVM-critical configurations are related to Java garbage collection.
Type of garbage collector to run
Configuration |
Description |
-xx:+useserialgc |
Serial garbage collector |
-xx:+useparallelgc |
Parallel garbage collector |
-xx:+useconcmarksweepgc |
Concurrent Tag Scan garbage collector |
-xx:parallelcmsthreads= |
Concurrent tag Scan garbage collector = number of threads used |
-xx:+useg1gc |
G1 garbage collector |
Optimized configuration of GC
Configuration |
Description |
-xms |
Initialize heap memory size |
-xmx |
Heap Memory Maximum Value |
-xmn |
Cenozoic size |
-xx:permsize |
Initialize permanent generation size |
-xx:maxpermsize |
Maximum capacity for permanent generation |
Examples of using JVM GC parameters
1 |
java -Xmx12m -Xms3m -Xmn1m -XX:PermSize=20m -XX:MaxPermSize=20m -XX:+UseSerialGC -jar java-application.jar |
In the next section of the Java garbage Collection tutorial, we will use a Java program to demonstrate how to monitor and analyze garbage collection.
Original link: javapapers translation: importnew.com - Nice
Link: http://www.importnew.com/13827.html
Java GC Series (3): Types of garbage collector