Detailed Java garbage collection mechanism →types of Java garbage collectors

Source: Internet
Author: User

Detailed Java garbage collection mechanism →types of Java garbage collectors transferred from: 1190000006214497

This article is not original, translated from types of Java garbage collectors
Allocating and freeing memory space for an object in Java is done automatically by the garbage collection thread. Unlike the C language, Java programmers do not need to manually write garbage collection-related code. This is one of the advantages of making Java so popular and Java helping programmers to write better Java applications.

This article will cover a variety of different types of Java garbage collector. Garbage collection is an automated process that Java frees programmers from the trivial tasks of allocating and freeing memory.
Java has four types of garbage collector,

    1. Serial Garbage Collector

    2. Parallel Garbage Collector

    3. CMS Garbage Collector

    4. G1 Garbage Collector


Each of these four types of garbage collector has its own advantages and disadvantages. The most important thing is that 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 scenarios can vary widely. It is therefore important to understand the various types of garbage collector and their application scenarios.

Serial Garbage Collector

The serial garbage collector controls all application threads. It is designed for single-threaded scenarios and uses only one thread to perform garbage collection work. It pauses all application threads to perform garbage collection work in a way that does not apply to the server's application environment. It is most suitable for simple command-line applications.

Use the-XX:+USESERIALGCJVM parameter to turn on the use of the serial garbage collector.

Parallel Garbage Collector

The 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 is also necessary to suspend all application threads when performing garbage collection.

CMS Garbage Collector

The concurrent tag cleanup (Concurrent Mark Sweep,cms) garbage collector uses multiple threads to scan the heap memory and mark objects that can be purged, and then clear the tagged objects. The CMS garbage collector pauses worker threads only in the following two scenarios.

    1. When the reference object is tagged in the old age

    2. In the process of garbage collection, there are changes in the heap memory.

Compared with the parallel garbage collector, the CMS collector uses more CPUs to guarantee 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 turn on using the CMS garbage collector.

G1 Garbage Collector

The G1 garbage collector is applied to large heap memory space. It divides the heap memory space into different areas and recycles the regions in parallel. G1 also immediately heap free space after reclaiming memory space to do the integration work to reduce fragmentation. CMS is performing memory consolidation work when all stops (Stop the WORLD,STW). For different areas G1 the priority based on the amount of garbage.

Use the-XX:USEG1GCJVM parameter to turn on the use of the G1 garbage collector.

Optimizations for Java 8

When using the G1 garbage collector, turn on the use-XX:+USESTRINGDEDUPLACATONJVM parameter. It optimizes heap memory consumption 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 decide on the application scenario, hardware configuration and throughput requirements.

Garbage Collection JVM Options

Here are some of the main JVM options related to 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 The largest permanent generation of the size

Example Usage of the JVM GC Options

java -Xmx12m -Xms3m -Xmn1m -XX:PermSize=20m -XX:MaxPermSize=20m -XX:+UseSerialGC -jar java-application.jar

Detailed Java garbage collection mechanism →types of Java garbage collectors

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.