Technology Sharing

[JVM Basics] Introduction to Java Garbage Collector

2024-07-11

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Garbage collector (garbage collection algorithm implementation)

The garbage collector is a specific implementation of the garbage collection algorithmSince the garbage collector is divided into young generation and old generation, in addition to G1 (which can control both the new generation and the old generation),The new generation and old generation garbage collectors must be used in pairs according to the requirements of hotspot.(You need to choose the corresponding combination according to the JDK version)

The specific combination relationship is as follows:

insert image description here

In the JVM underlying source code, in some special cases, the CMS collector will call the Serial Old collector

Arthas checks the garbage collector used

You cannot directly see what garbage collector is used, you can only infer it based on the algorithm

insert image description here

Young Generation-Serial Garbage Collector

Serial is aSingle-threaded serial collection of the young generationThe garbage collector.

insert image description here

Recycling Era and Algorithm

  • Young Generation
  • Replication Algorithm

advantage

Excellent throughput on a single CPU processor

shortcoming

The throughput under multiple CPUs is not as good as other garbage collectors (single thread, only one CPU is used). If the heap is too large, the user thread will wait for a long time.

Applicable scene

Client programs written in Java or scenarios with limited hardware configuration (few CPU cores)

how to use

-XX: UseSerialGC: Both the new generation and the old generation use serial collectors.

Old Generation-SerialOld Garbage Collector

SerialOld is the old version of the Serial garbage collector, usingSingle-threaded serial recycling

insert image description here

Recycling Era and Algorithm

  • Old Generation
  • Mark-Clear Algorithm

advantage

Excellent throughput on a single CPU processor

shortcoming

The throughput is not as good as other garbage collectors under multi-CPU conditions. If the heap is too large, the user thread will wait for a long time.

Applicable scene

Used with the Serial garbage collector or in special cases with CMS

how to use

-XX: UseSerialGC: Both the new generation and the old generation use serial collectors.

Young Generation-ParNew Garbage Collector

The ParNew garbage collector is essentiallyOptimize Serial under multiple CPUs, using multithreaded garbage collection

insert image description here

Recycling era and algorithm:

  • Young Generation
  • Replication Algorithm

advantage

  • Short pause time under multiple CPU processors

shortcoming

  • The throughput and pause time are not as good as G1, so it is not recommended after JDK9.

Applicable scene

  • In JDK8 and earlier versions, it is used with the CMS old generation garbage collector

how to use

  • -XX: UseParNewGC: The new generation uses the ParNew collector, and the old generation uses the serial collector

insert image description here

Old Generation - CMS (Concurrent Mark Sweep) Garbage Collector

The CMS garbage collector focuses on the system's pause time (minimizing STW and optimizing user experience).Allow user threads and garbage collection threads to execute concurrently in certain steps, reducing the waiting time of user threads.

Recycling Era and Algorithm

  • Old Generation
  • Mark-and-Sweep Algorithm

advantage

  • The system has a shorter pause time due to garbage collection, and the user experience is good

shortcoming

1. Memory fragmentation problem

2. Degeneration problem (in some specific cases, it will degenerate into a single-threaded collector such as SerialOld)

3. Floating garbage problem (some garbage cannot be recycled during the recycling process)

Applicable scene

Scenarios where users request large amounts of data and high frequency in large Internet systems, such as order interfaces and product interfaces

use

XX: UseConcMarkSweepGC, you can set the collectors for the young generation and the old generation separately

insert image description here

insert image description here

CMS Execution Steps

  1. Initial marking, marking in a very short timeObjects that GC Roots can be directly associated with
  2. Concurrent marking, Mark all objects, the user thread does not need to be paused. (Although concurrent marking is executed together with the user thread, if the concurrent marking occupies a high number of resources, it will also affect the user thread)
  3. Re-marking (concurrent). Since some objects will change during the concurrent marking phase, there may be mis-marking (the object used was originally alive, but after marking, the user thread made it dead, resulting in mis-marking) and missed marking (because it is concurrent, some objects may be just created by the user thread, which will lead to missed marking), so re-marking is required.
  4. Concurrent cleanup, clean up dead objects, user threads do not need to be paused.

Note: STW only occurs during the initial marking and re-marking phases

insert image description here

shortcoming:

1. CMS uses the mark-sweep algorithm. After the garbage collection is completed, a large amount of memory fragmentation will appear. In order not to affect the allocation of objects,CMS will defragment during Full GCThis will cause the user thread to be suspended.You can use the -XX:CMSFullGCsBeforeCompaction=N parameter (default 0) to adjust the compaction after N Full GCs.

2. It is impossible to handle the "floating garbage" generated during the concurrent cleaning process and cannot achieve complete garbage collection (during this cleaning process, the user thread concurrently created some objects, but they were not used again soon. These objects were not recycled in this cleaning and need to wait until the next cleaning, so they are called floating garbage).

3. If there is insufficient memory in the old generation to allocate objects, CMS will degenerate into Serial Old single-threaded recycling of the old generation.

Number of concurrent threads:

The number of threads during the concurrent phase of CMS can be set by-XX:ConcGCThreadsParameter settings are calculated by the system using the following formula:(-XX:ParallelGCThreads定义的线程数 3) / 4, ParallelGCThreads is the number of parallel threads after the STW pause

ParallelGCThreads is determined by the number of processor cores:

1. When the number of CPU cores is less than 8, ParallelGCThreads = number of CPU cores

2. Otherwise ParallelGCThreads = 8 (CPU cores – 8)*5/8

My computer has 12 logical processors, so ParallelGCThreads = 8 (12 - 8) * 5/8 = 10, ConcGCThreads = (number of threads defined by -XX:ParallelGCThreads 3) / 4 = (10 3) / 4 = 3

insert image description here

Finally, we can get this picture:

insert image description here

The concurrent marking and concurrent cleaning phases will be processed in parallel using 3 threads. The re-marking phase will be processed using 10 threads. Due to the limited number of CPU cores, the concurrent phase will affect the performance of user thread execution.

insert image description here

Young Generation-Parallel Scavenge Garbage Collector

Parallel Scavenge isJDK8 default young generation garbage collector,MultithreadingParallel RecyclingFocus on system throughputIn order to increase throughput, PS willAutomatically adjust the heap memory size (adjust the new generation, old generation memory size, and promotion threshold)

insert image description here

Recycling Era and Algorithm

  • Young Generation
  • Replication Algorithm

advantage

  • The throughput is high, and it supports manual setting of parameters to control the throughput. In order to improve the throughput, the virtual machine will dynamically adjust the heap parameters (the user only needs to set the throughput, not other parameters such as memory size)

shortcoming

  • The single pause time cannot be guaranteed, but the STW time can be set.

Applicable scene

  • Background tasks do not require user interaction and are prone to generate a large number of objects. For example: processing of large data, exporting large files

Common parameters

Parallel Scavenge allows you to manually set the maximum pause time and throughput. Oracle officially recommends not setting the maximum heap memory value when using this combination. The garbage collector will automatically adjust the memory size based on the maximum pause time and throughput.

  • Maximum pause time,-XX:MaxGCPauseMillis=n Set the maximum number of milliseconds to pause before each garbage collection.
  • Throughput,-XX:GCTimeRatio=n Set the throughput to n (user thread execution time = n/(n 1))
  • Automatically adjust memory size, -XX: UseAdaptiveSizePolicyThe setting allows the garbage collector to automatically adjust the memory size based on the throughput and the maximum number of milliseconds of pause. This parameter is enabled by default (Oracle recommends that when using the PS combination, do not set the maximum value of the heap memory and let the garbage collector adjust it automatically)

Note: The maximum pause time and throughput are conflicting indicators. The garbage collector will try to meet the maximum pause time (sometimes it is set too small and cannot be met, and will exceed the set maximum pause time), sacrificing throughput. If you want to set the maximum pause time and throughput at the same time, do more tests to make them more coordinated.

Old Generation-Parallel Old Garbage Collector

Parallel Old is an old generation version designed for the Parallel Scavenge collector, which uses multi-threaded concurrent collection.

insert image description here

Recycling Era and Algorithm

  • Old Generation
  • Mark-sweep algorithm (actually mark-sweep)

advantage

  • Concurrent collection, more efficient on multi-core CPUs

shortcoming

  • The pause time will be longer

Applicable scene

  • Use with Parallel Scavenge

how to use

JDK8 sets the parameters to use this collector by default

parameter:-XX: UseParallelGC or-XX: UseParallelOldGCYou can use a combination of Parallel Scavenge Parallel Old.
insert image description here

insert image description here

test

-XX: PrintFlagsFinal: You can print the final values ​​of all configuration items when the program starts, to see if the automatic adjustment function is turned on

insert image description here

insert image description here

insert image description here

insert image description here

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 垃圾回收器案例3
 */
//-XX: UseSerialGC -Xmn1g -Xmx16g -XX:SurvivorRatio=8  -XX: PrintGCDetails -verbose:gc -XX: PrintFlagsFinal
//-XX: UseParNewGC  -Xmn1g -Xmx16g -XX:SurvivorRatio=8  -XX: PrintGCDetails -verbose:gc
//-XX: UseConcMarkSweepGC
//-XX: UseG1GC   -Xmn8g -Xmx16g -XX:SurvivorRatio=8  -XX: PrintGCDetails -verbose:gc MaxGCPauseMillis
//-XX: PrintFlagsFinal  -XX:GCTimeRatio = 19  -XX:MaxGCPauseMillis=10 -XX: UseAdaptiveSizePolicy
public class GcDemo2 {

    public static void main(String[] args) throws IOException {
        int count = 0;
        List