Introduction to the

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

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

Those GC defaults

There are a lot of parameters in GC or JVM that control memory usage:

2. Having a controlled JIT:

Some control generation ratio, and some control GC concurrency:

Of course, most of the parameters do not need to be adjusted, and the JVM does a good job of setting them dynamically.

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

The choice of the GC

We know that there are many types of GC in the JVM, and the different GC choices can have a significant impact on the performance of Java programs.

After JDK9, G1 is the default garbage collector.

Let’s take a look at G1’s tuning parameters.

G1 is based on generational technology, but the JVM is still developing some GC algorithms that are not based on generational technology, such as ZGC, and we can choose the GC algorithm that suits us according to our needs.

Maximum number of GC threads

GC is performed by dedicated GC threads, not the more GC threads the better, the maximum number of default threads is dynamically determined by heap size and available CPU resources.

Of course you can modify the GC thread with the following two options:

-xx :ParallelGCThreads=threads Sets the number of garbage collection threads for STW. -xx :ConcGCThreads = n Sets the number of parallel flag threadsCopy the code

Generally ConcGCThreads can be set to 1/4 of ParallelGCThreads.

Initialize the heap size

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

You can use

 -XX:InitialHeapSize=size
Copy the code

To reset.

Maximum heap size

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

You can use:

 -XX:MaxHeapSize
Copy the code

To reset.

Hierarchical compilation technique

Hierarchical compilation is enabled by default. You can use:

 -XX:-TieredCompilation
Copy the code

To turn off hierarchical compilation. If hierarchical compilation is enabled, you may want to be concerned about the impact of the C1 and C2 compilers in the JIT.

What do we really want

Fish, I want, bear PAWS also I want; You can’t have both. He who gives up the fish takes the bear’s paw. – mencius

GC occurs many times during the execution of a Java program, so we actually have two statistical caliber:

  1. Maximum pause-time Goal (Maximum pause-time Goal)
  2. Throughput Goal Ratio of total time spent on GC to application execution time

Maximum pause time

The pause time for a single GC is a statistical average, because the time for a single GC is not controllable, but by taking an average, the GC can dynamically adjust the heap size, or some other GC parameter, to ensure that the time for each GC does not exceed this average.

We can do this by setting:

-XX:MaxGCPauseMillis=<nnn>
Copy the code

To control this value.

Regardless of how this parameter is set, the overall number of objects that need to be GC is fixed, and if the single GC pause time is short, the heap size may need to be reduced and fewer objects will be collected. This will result in an increase in the frequency of GC. As a result, the total time of GC increases and the Throughput of the program is affected.

throughput

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

We can do this by setting:

-XX:GCTimeRatio=nnn
Copy the code

To control.

If the throughput goal is not met, then the GC may increase the heap size to reduce the frequency of GC execution. However, this increases the Maximum pause-time for a single Time.

If both throughput and maximum pause-time parameters are set, the JVM will try to dynamically reduce the heap size until one of the targets is not met.

In contrast, G1 is more focused on maximum pause time, while ZGC is more focused on throughput.

Author: Flydean program stuff

Link to this article: www.flydean.com/jvm-diagnos…

Source: Flydean’s blog

Welcome to pay attention to my public number: procedures those things, more wonderful waiting for you!