Common GC invocation ideas:

Analysis scenario:

General analysis scenarios are as follows: startup speed; The occasional slower-than-average response or sluggishness.

Identify the target

Memory footprint, low latency, throughput.

Collect the logs

Collect GC logs through parameter configuration and check GC status using JDK tools

1, configure GC log: Java -xmx1024m-xloggc :gc1.log -jar xxx.jar

2. After the configuration is started, gc1.log is generated in the folder. More folder. You can see:

From the logs, you can see some GC log information, such as initialization heap size and maximum stack size.

Analysis of the log

Use tools to assist log analysis and check the number and time of GC

1, we use the GCviewer tool to check the GC situation GCviewer download address: github.com/chewiebug/G…

2. Look at the picture and speak:

From the figure, we can see that the total pause time for GC is 27.9s. The number of pauses was 9,652. The Full GC time passed 1.19s and the frequency was only 45 times.

So let’s look at the statistics:

From the figure, we know that the total pause time is 29.1s and the number of pauses is more than 9,000 times. The average data is shown in the figure.

Let’s take a look at memory:

Real-time analysis

From the real-time data: the total time of GC is 13.789s to 14.181s, during which 7 times are run, with an average of 0.056s. The number of yGC is 3340-3250 = 90, and the time is 12.963-12.571=0.392s, so the average YGC time is 0.004. Now let’s look at the non-default configuration:

Garbage collector Parallel parameter tuning

Because there are more than 800 JVM configurations, tuning is typically followed by a logging manual that records which configurations are configured.

The JDK’s default collector is UseParallelGC, which is throughput first

  • Turn on the adaptive GC policy

Check whether it is enabled first:

  • Sets the number of threads used for garbage collection

First look at the current thread count configuration:

Configure the number of GC threads and then look at the GC information to optimize the configuration.

Java -XMX1024M - -xx :ParallelGCThreads=2 jar Performance 0.0.1 -snapshot.jarCopy the code
  • GC collector configuration
java -XX:+PrintFlagsFinal -version|grep UseParallelGC
Copy the code

You can see that parallelGC is used by default

Now let’s change to: CMS GC mode

Java -XMx1024m - -xx :+UseConcMarkSweepGC jar performance 0.0.1- snapshot.jarCopy the code

And then we look at what happens to GC.

Conclusion:

Tuning GC is a tedious process. Generally, the program is optimized through code. Continuous GC tuning of the JVM is only done in specific scenarios.