2024-07-11
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
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:
In the JVM underlying source code, in some special cases, the CMS collector will call the Serial Old collector
You cannot directly see what garbage collector is used, you can only infer it based on the algorithm
Serial is aSingle-threaded serial collection of the young generationThe garbage collector.
Excellent throughput on a single CPU processor
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.
Client programs written in Java or scenarios with limited hardware configuration (few CPU cores)
-XX: UseSerialGC
: Both the new generation and the old generation use serial collectors.
SerialOld is the old version of the Serial garbage collector, usingSingle-threaded serial recycling
Excellent throughput on a single CPU processor
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.
Used with the Serial garbage collector or in special cases with CMS
-XX: UseSerialGC
: Both the new generation and the old generation use serial collectors.
The ParNew garbage collector is essentiallyOptimize Serial under multiple CPUs, using multithreaded garbage collection
-XX: UseParNewGC
: The new generation uses the ParNew collector, and the old generation uses the serial collectorThe 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.
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)
Scenarios where users request large amounts of data and high frequency in large Internet systems, such as order interfaces and product interfaces
XX: UseConcMarkSweepGC
, you can set the collectors for the young generation and the old generation separately
Note: STW only occurs during the initial marking and re-marking phases
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:ConcGCThreads
Parameter 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
Finally, we can get this picture:
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.
Parallel Scavenge isJDK8 default young generation garbage collector,MultithreadingParallel Recycling,Focus 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)。
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.
-XX:MaxGCPauseMillis=n
Set the maximum number of milliseconds to pause before each garbage collection.-XX:GCTimeRatio=n
Set the throughput to n (user thread execution time = n/(n 1))-XX: UseAdaptiveSizePolicy
The 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.
Parallel Old is an old generation version designed for the Parallel Scavenge collector, which uses multi-threaded concurrent collection.
JDK8 sets the parameters to use this collector by default
parameter:-XX: UseParallelGC
or-XX: UseParallelOldGC
You can use a combination of Parallel Scavenge Parallel Old.
-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
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