Java GC - Types of Garbage Collectors

Reprinted from: http://www.importnew.com/13827.html

In this tutorial we will learn about several existing garbage collectors. In Java, garbage collection is an automatic process that can replace the complicated work of memory allocation and reclamation by programmers. This is the third article in the Garbage Collection Tutorial series. In the previous part 2 we saw how garbage collection works in Java. It was an interesting article and I recommend you to read it. The first part introduces Java's garbage collection, mainly JVM architecture, heap memory model and some Java terminology.

Java has four types of garbage collectors:

  1. Serial Garbage Collector
  2. Parallel Garbage Collector
  3. Concurrent Mark Scan Garbage Collector (CMS Garbage Collector)
  4. G1 Garbage Collector

Each type has its own advantages and disadvantages. The important thing is that we can choose the type of garbage collector through the JVM when we are programming. We choose by passing arguments to the JVM. Each type is largely different and can give us completely different application performance. It is important to understand each type of garbage collector and make the right choice based on 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, uses only a single thread for garbage collection, and works by freezing all application threads, so may not be suitable for server environments. It is best suited for simple command line programs.

-XX:+UseSerialGCThe serial garbage collector is available via JVM arguments .

2. Parallel garbage collector

Parallel garbage collectors are also called throughput collectors. It is the default garbage collector of the JVM. Unlike the serial garbage collector, it uses multiple threads for garbage collection. Similarly, it also freezes all application threads while performing garbage collection

3. Concurrent mark scan garbage collector

Concurrent marked garbage collection uses multiple threads to scan heap memory, mark instances for cleaning and clean up marked instances. The concurrent marking garbage collector will only hold all threads of the application in the following two cases.

  1. When the marked reference object is in the tenured area;
  2. During garbage collection, the data in the heap memory is changed concurrently.

Compared to the parallel garbage collector, the concurrent mark-scan garbage collector uses more CPU to ensure the throughput of the program. If we can allocate more CPU for better program performance, then concurrent mark-on-scan garbage collector is a better choice than concurrent garbage collector.

XX:+USeParNewGC Turn on concurrent mark-scanning garbage collector via JVM argument  .

4. G1 garbage collector

The G1 garbage collector is suitable for large heap memory. It divides the heap memory into different areas and performs garbage collection on them concurrently. G1 can also compress the remaining heap memory space after reclaiming memory. Concurrent scan marks the garbage collector to compact memory in the STW case. G1 garbage collection will give priority to the first area with the most garbage

–XX:+UseG1GC Using the G1 garbage collector via JVM arguments 

What's New in Java 8

When using the G1 garbage collector, pass JVM parameters  -XX:+UseStringDeduplication . We can optimize heap memory by removing duplicate strings and keeping only one char[]. This option was introduced in Java 8 u 20.

We present all four Java garbage collectors, and the decision to use depends on the application, hardware performance, and throughput requirements.

JVM configuration for garbage collection

The following key JVM configurations are all related to Java garbage collection.

Type of garbage collector running

configure describe
-XX:+UseSerialGC serial garbage collector
-XX:+UseParallelGC Parallel garbage collector
-XX:+UseConcMarkSweepGC Concurrent Mark Scan Garbage Collector
-XX:ParallelCMSThreads= concurrent mark scan garbage collector = number of threads used
-XX:+UseG1GC G1 garbage collector

Optimized configuration of GC

configure describe
-Xms Initialize heap memory size
-Xmx Maximum heap memory
-Xmn young generation size
-XX:PermSize Initialize permanent generation size
-XX:MaxPermSize permanent generation maximum capacity

Example using JVM GC parameters

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

In the next part 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  Mr. Hao Hao's
translation link:  http://www.importnew.com/13827.html
For reprinting, please keep the original source, translator and translation link. ]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326132938&siteId=291194637