Reprint: http://youli9056.github.io/blog/types-of-java-garbage-collectors/
This article is the third of the Java Garbage Collection series, not original, translated from types of Java garbage collectors. Without the basics, read the first two Java garbage Collection Introduction (the architecture of the JVM, the heap memory model and the surrounding Java terminology) and how Java garbage before reading this article. Collection works? (The overview Describes how the GC works)
This article will describe the various types of Java garbage collector. Garbage collection is the automated process that Java uses to liberate programmers from the trivial tasks of allocating and freeing memory.
Java has four types of garbage collector serial garbage Collector Parallel garbage Collector CMS garbage Collector G1 Garbage Collector
These four types of garbage collector have their own advantages and disadvantages. Most importantly, programmers can choose which type of garbage collector the JVM uses. We can set which one to use by passing different JVM parameters. The efficiency of each garbage collector in different application scenarios can vary greatly. It is therefore important to understand the various types of garbage collector and their application scenarios. 1. Serial Garbage Collector
The serial garbage collector controls all application threads. It is designed for single-threaded scenarios, using only one thread to perform garbage collection work. The way it suspends all application threads to perform garbage collection does not apply to the server's application environment. It is most suitable for simple command-line programs.
Use the-XX:+USESERIALGCJVM parameter to turn on the use of the serial garbage collector. 2. Parallel Garbage Collector
Parallel garbage collector is also known as a throughput based collector. It is the default garbage collector for the JVM. Unlike serial, it uses multiple threads to perform garbage collection work. As with the serial collector, it also needs to suspend all application threads while performing garbage collection. 3. CMS Garbage Collector
The Concurrency Mark Cleanup (Concurrent Mark Sweep,cms) garbage collector uses multiple threads to scan heap memory and mark objects that can be purged, and then clear the tagged objects. The CMS garbage collector suspends worker threads only in the following two scenarios, and in the old days when a reference object is marked, changes occur in heap memory during garbage collection
Compared with parallel garbage collector, the CMS collector uses more CPUs to ensure higher throughput. If we can have more CPUs to improve performance, then the CMS garbage collector is a better choice than a parallel collector.
Use the-XX:+USEPARNEWGCJVM parameter to open the CMS garbage collector. 4. G1 Garbage Collector
The G1 garbage collector is applied to large heap memory space. It divides the heap memory space into different regions and makes the recycling work in parallel to each area. G1 also immediately heap free space to do the consolidation work to reduce fragmentation after the memory space is reclaimed. CMS performs memory consolidation when all stops (Stop the WORLD,STW). For different areas, the G1 determines the priority according to the amount of garbage.
Use the-XX:USEG1GCJVM parameter to open the use of the G1 garbage collector.
Optimization of Java 8
When using the G1 garbage collector, open the use-XX:+USESTRINGDEDUPLACATONJVM parameter. It optimizes heap memory usage by moving duplicate string values to the same char[] array. This is the option introduced by Java 8 U 20.
The above four Java garbage collector, when to use which to determine the application scenario, hardware configuration and throughput requirements. garbage Collection JVM Options
Here are some of the main JVM options associated with Java garbage collection. Type of Garbage Collector to run
Options |
Description |
-xx:+useserialgc |
Serial garbage collector |
-xx:+useparallelgc |
Parallel garbage collector |
-xx:+useconcmarksweepgc |
CMS garbage collector |
-xx:parallescmsthread= |
CMS garbage Collector-Number of threads used |
-xx:useg1gc |
G1 garbage collector |
GC Optimization Options
Options |
Description |
-xms |
Initial heap Memory size |
-xmx |
Maximum heap memory size |
-xmn |
The size of the young generation |
-xx:permsize |
The size of the initial permanent generation |
-xx:maxpermsize |
Size of the largest permanent generation |
Example Usage of JVM GC Options
Java-xmx12m-xms3m-xmn1m-xx:permsize=20m-xx:maxpermsize=20m-xx:+useserialgc-jar JAVA-APPLICATION.J