Troubleshoot: using jfr to analyze performance problems

brief introduction

The performance analysis of Java program is a very difficult problem. Especially for a very complex program, analysis is a headache.

Fortunately, the JVM has introduced jfr, which can be used to monitor and analyze various events of the JVM. Through the analysis of these events, we can find out the potential problems.

Today, let's introduce some jfr events that are important for Java performance analysis.

GC performance events

Generally speaking, GC will have an important impact on the performance and operation of Java programs. We can use jfr to monitor JDK Gcphasepause event.

Here is a JDK Example of gcphasepause:

jfr print --events jdk.GCPhasePause flight_recording_1401comflydeanTestMemoryLeak89268.jfr

Output results:

jdk.GCPhasePause {
  startTime = 19:51:49.798
  duration = 41.1 ms
  gcId = 2
  name = "GC Pause"
}

Through the gcphasepause event, we can count the total GC pause time and the average time of each GC pause.

Generally speaking, GC is executed in the background, so we don't need to pay attention to the execution time of GC itself, because it will not affect the performance of the program. We need to focus on the application because of the GC pause time.

Consider the following two cases. The first one is that a separate GC causes the GC pause time to be too long. The second is that the total GC pause time is too long.

If this is the first case, you may need to consider changing a GC type, because different GC types will directly have different processing in the balance of pause time and throughput. At the same time, we need to reduce the use of finalizers.

If it is the second case, we can solve it from the following aspects.

Synchronization performance

In a multithreaded environment, because multithreading will compete for shared resources, the synchronization of resources or the use of locks will affect the performance of the program.

We can monitor JDK Javamonitorwait event.

jfr print --events jdk.JavaMonitorWait flight_recording_1401comflydeanTestMemoryLeak89268.jfr

Let's look at a result:

jdk.JavaMonitorWait {
  startTime = 19:51:25.395
  duration = 2 m 0 s
  monitorClass = java.util.TaskQueue (classLoader = bootstrap)
  notifier = N/A
  timeout = 2 m 0 s
  timedOut = true
  address = 0x7FFBB7007F08
  eventThread = "JFR Recording Scheduler" (javaThreadId = 17)
  stackTrace = [
    java.lang.Object.wait(long)
    java.util.TimerThread.mainLoop() line: 553
    java.util.TimerThread.run() line: 506
  ]
}

By analyzing the java monitor wait event, we can find the most competitive lock for further analysis.

IO performance

If an application has many IO operations, IO operations are also a key link that will affect performance.

We can monitor two IO types: socket IO and file IO.

The corresponding events are: DK socketWrite,jdk. socketRead,jdk. FileWrite,jdk. FileRead。

Performance of code execution

The code is run through the CPU. If the CPU is used too much, it may also affect the performance of the program.

We can monitor JDK Cpuload event to analyze cpuload.

jfr print --events jdk.cpuLoad flight_recording_1401comflydeanTestMemoryLeak89268.jfr

See the operation results:

jdk.cpuLoad {
  startTime = 19:53:25.519
  jvmUser = 0.63%
  jvmSystem = 0.37%
  machineTotal = 20.54%
}

If the JVM uses less CPU, but the CPU utilization of the whole machine is relatively high, it indicates that other programs are occupying CPU.

If the JVM's own CPU usage is high, you need to find the CPU consuming thread for further analysis.

Other useful events

In addition to the events mentioned above, there are other useful events that we can focus on.

For example, thread related: JDK ThreadStart,jdk. ThreadEnd,jdk. ThreadSleep,jdk. ThreadPark。

If you use JMC, you can intuitively view various events of jfr.

Therefore, JMC is recommended.

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
分享
二维码
< <上一篇
下一篇>>