Eight pictures to thoroughly understand jdk8 GC tuning script – PDF download
brief introduction
There are many JVM parameters. According to my statistics, there are 1853 JVM parameters in jdk8 and 680 formal parameters.
So many parameters give us fine-grained control over the JVM, but not all parameters need to be adjusted by ourselves. We need to pay attention to some of the most commonly used GC parameters that have a great impact on performance.
In order to better understand the secrets of GC tuning in jdk8, eight diagrams are specially prepared here. At the end of this article, a summary PDF all in one document is attached. Download the PDF and look at it when you encounter problems. Isn't it beautiful?
Memory structure of generational garbage collector
In order to improve the efficiency of GC, modern JVMs adopt generational garbage collection strategy (ZGC is not).
Java runtime memory can be divided into JVM memory and non JVM memory.
JVM memory can be divided into heap memory and non heap memory.
Everyone is familiar with heap memory. Eden, survivor and oldgen in younggen.
Thread stack, code cache, NiO direct buffers, Metaspace, etc. are stored in non heap memory.
Note that the Metaspace meta space here is the implementation of the method area in jdk8, which is allocated in local memory.
GC available in jdk8
What GC can be used in jdk8?
Here, we take the hotspot JVM as an example. A total of four GC methods can be used:
For parallelgc and CMS GC, the GC mode can be set for the young generation and the old generation respectively.
You may have a question when you see the above figure. What is the difference between parallel scavenge and parallel scavenge?
In fact, the algorithms of the two GCs are similar. The parallel scavenge collector is often called the "throughput first" collector. The parallel scavenge collector provides two parameters to accurately control the throughput- 20: Maxgcpausemillis: controls the maximum garbage collection pause time- 20: Gctimeratio: sets the throughput size.
At the same time, the parallel scavenge collector can cooperate with the adaptive adjustment strategy to hand over the tuning task of memory management to the virtual machine.
Print GC information
If you want to study and understand the internal information of GC, GC information printing is indispensable:
The figure above provides some very useful GC log control parameters.
Memory tuning parameters
The JVM is divided into heap area and non heap area. Each area is further divided. The following are the parameters for adjusting the size of each area:
Thread configuration
TLAB, do you remember? The full name of TLAB is thread local allocation buffers. TLAB is a continuous space allocated in Eden interval. These contiguous spaces are then allocated to individual threads.
Because each thread has its own independent space, the concept of synchronization is not involved here.
The above figure shows the parameters of TLAB.
General GC parameters
Although jdk8 has so many GC, they have some general GC parameters:
I don't know how to translate young space tenuring. This mainly refers to how many times the objects in young space will be promoted to old space after GC.
CMS GC
The full name of CMS is concurrent mark sweep. Is a very, very complex GC.
How complicated is it? There are more than 100 CMS tuning parameters!
The following figure shows the parameters of common CMS.
CMS will not be discussed here, because after jdk9, CMS has been abandoned.
The main reason is that CMS is too complex. It requires a huge workload to be downward compatible, and then it is directly abandoned.
After jdk9, the default GC is G1.
G1 parameter
The G1 collector is generational and REGIONIZED, that is, the whole heap memory is divided into a series of regions of equal size. At startup, the JVM sets the size of regions. Depending on the heap size, the size of regions can vary from 1MB to 32MB, and the maximum number of regions is 2048. Eden District, survivor district and old age are logical collections of these regions, which are not continuous.
Garbage collection process in G1: young generation collection and mixed collection are carried out alternately, with a global concurrent marking cycle behind it. When the space occupied by the old partition reaches or exceeds the initial threshold, the concurrent marking cycle will be triggered.
The following figure shows the tuning parameters of G1: