Troubleshoot: what exactly is GC tuning

brief introduction

We often hear that we even need to do GC tuning ourselves. So what is the purpose of GC tuning? Make the program run faster? Let GC consume less resources? Or make the program more stable?

Read this article with these questions, and you will get a systematic or even different result.

Default values for those GCS

In fact, there are many GC or JVM parameters that control memory usage:

With JIT control:

Some control the generation ratio and some control GC Concurrency:

Of course, most of the parameters do not need to be adjusted by ourselves. The JVM will dynamically help us set the values of these variables.

If we don't set these values, what are the parameters that affect GC performance comparison and their default values?

GC selection

We know that there are many kinds of GC in the JVM, and different GC choices still have a great impact on the performance of Java programs.

After jdk9, G1 is the default garbage collector.

Let's look at the tuning parameters for G1.

G1 is based on generational technology. In fact, the JVM is still developing some GC algorithms that are no longer based on generational technology, such as ZGC. We can choose the GC algorithm suitable for us according to our needs.

Maximum number of GC threads

GC is executed by special GC threads, which does not mean that the more GC threads, the better. The maximum value of this default thread is dynamically determined by the heap size and available CPU resources.

Of course, you can use the following two options to modify the GC thread:

 -XX:ParallelGCThreads=threads 设置STW的垃圾收集线程数

 -XX:ConcGCThreads = n 设置并行标记线程的数量

Generally, concgcthreads can be set to 1 / 4 of parallelgcthreads.

Initialize heap size

By default, the initialized heap size is 1 / 64 of the physical memory.

You can use

 -XX:InitialHeapSize=size

To reset.

Maximum heap size

By default, the maximum heap size is 1 / 4 of the physical memory.

You can use:

 -XX:MaxHeapSize

To reset.

Layered compilation technology

Layered compilation technology is on by default. You can use:

 -XX:-TieredCompilation

To turn off hierarchical compilation. If layered compilation is enabled, you may need to pay attention to the impact of C1 and C2 compilers in JIT.

What the hell do we want

GC occurs many times during the running of Java programs, so we actually have two statistical caliber:

Maximum pause time

The pause time of a single GC is a statistical average value, because the time of a single GC is actually uncontrollable, but after taking the average value, the GC can dynamically adjust the size of the heap or some other GC parameters, so as to ensure that the time of each GC will not exceed this average value.

We can set:

-XX:MaxGCPauseMillis=<nnn>

To control this value.

No matter how this parameter is set, the objects that need to be GC are fixed. If the pause time of a single GC is short, the size of the heap size may need to be reduced, and fewer objects will be recycled. This will result in an increase in the frequency of GC. This leads to an increase in the total time of GC and affects the throughput of the program.

Throughput

Throughput is determined by the ratio of the time spent on the GC to the time spent on the application.

We can set:

-XX:GCTimeRatio=nnn

To control.

If the goal of throughput is not reached, the GC may increase the heap size to reduce the execution frequency of the GC. However, this will increase the maximum pause time for a single time.

If the parameters of throughput and maximum pause time are set at the same time, the JVM will try to dynamically reduce the size of heap size until one of the goals cannot be met.

Relatively speaking, G1 focuses more on the maximum pause time, while ZGC focuses more on throughput.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>