[JVM Monitoring Tool] Performance Diagnosis--Use of JProfiler

background

Performance diagnosis is a problem that software engineers often face and solve in their daily work. Today, when user experience is paramount, solving application performance problems can bring great benefits. As one of the most popular programming languages, Java's application performance diagnosis has been widely concerned by the industry. There are many factors that may cause performance problems in Java applications, such as thread control, disk read and write, database access, network I/O, garbage collection, and so on. To locate these problems, an excellent performance diagnostic tool is essential. This article will introduce the commonly used tools in the process of Java performance diagnosis, and focus on the basic principles and best practices of the excellent representative JProfiler (the research done in this article is based on jprofiler10.1.4).

1. Introduction to Java performance diagnostic tools

In the Java world, there are many diagnostic tools to choose from, including simple command line tools such as jmap and jstat, graphical comprehensive diagnostic tools such as JVisualvm and JProfiler, and distributed diagnostic tools such as SkyWalking and ARMS. Application performance monitoring system. They are introduced respectively below.

2. Simple command line tool

The JDK has a number of command-line tools built into it that can be used to obtain information on different aspects and levels of the target JVM.

  • jinfo - Used to view and adjust various parameters of the target JVM in real time.
  • jstack - Used to obtain thread stack information in the target Java process, which can be used to detect deadlocks, locate infinite loops, etc.
  • jmap - Used to obtain memory-related information of the target Java process, including the usage of each area of ​​the Java heap, statistical information of objects in the heap, class loading information, etc.
  • jstat - A lightweight multi-purpose monitoring tool that can be used to obtain information about class loading, JIT compilation, garbage collection, memory usage, etc. of a target Java process.
  • jcmd - A more comprehensive tool than jstat, which can be used to obtain performance statistics, JFR, memory usage, garbage collection, thread stack, JVM runtime and other information of the target Java process.

3. Graphical comprehensive diagnostic tool

Using the above command-line tools or a combination can help you get basic information about the performance of your target Java application, but they have the following limitations:

  1. Method-level analysis data cannot be obtained, such as the call relationship between methods, the number of calls and call time of each method, etc. (this is critical for locating application performance bottlenecks).
  2. Requiring the user to log in to the host machine where the target Java application is located is not very convenient to use.
  3. The analysis data is output through the terminal, and the result display is not intuitive enough.

The following introduces several graphical comprehensive performance diagnostic tools.

JVisualvm

JVisualvm is a built-in visual performance diagnostic tool of JDK. It obtains the analysis data of the target JVM through JMX, jstatd, Attach API, etc., including CPU usage, memory usage, thread stack information, etc. In addition, it can also visually display the number and size of each object in the Java heap, the number of calls and execution time of each Java method, and so on.

For more information, please refer to my previous article: [JVM Monitoring Tool] Use of JVisualVM

JProfiles

JProfiler is a Java application performance diagnostic tool developed by ej-technologies. It focuses on four important themes.

  • Method calls - Analysis of method calls can help you understand what your application is doing and find ways to improve its performance.
  • Memory allocation - By analyzing objects on the heap, reference chains and garbage collection, it can help you fix memory leaks and optimize memory usage.
  • Threads and Locks - JProfiler provides various analysis views for threads and locks to help you find multi-threading problems.
  • High Level Subsystems - Many performance issues occur at a higher semantic level. For example, for JDBC calls, you might want to find out which SQL statement is executing the slowest. JProfiler supports integrated profiling of these subsystems.

JConsole

Use JConsole to monitor processes, threads, memory, cpu, and classes

4. Distributed Application Performance Diagnosis

If you only need to diagnose the performance bottleneck of a stand-alone Java application, the diagnostic tools described above are sufficient. However, as modern system architecture gradually changes from monolithic to distributed and microservices, simply using the above tools often cannot meet the needs. At this time, it is necessary to rely on the full-link tracking functions provided by distributed tracking systems such as Jaeger, ARMS, and SkyWalking. There are many types of distributed tracing systems, but the implementation principles are similar. They record tracing information through code burying, transmit the recorded data to the central processing system through SDK or agent, and finally provide a query interface to display and analyze the results.

5. Set JProfiler in IDEA

What is JProfiler

JProfiler's intuitive GUI lets you find performance bottlenecks, catch memory leaks, and troubleshoot threading issues. It allows you to do root analysis of the resource collector on the heap walker, and you can easily find memory leaks; the heap snapshot (snapshot) mode allows unreferenced objects, slightly referenced objects, or finalization The objects in the queue will be removed; the wizard is integrated to analyze the browser's Java plug-in function.

Function

  • Local session real-time analysis mode
    Once the user defines the way to start the application, JProfiler can set it up, and the user can immediately see the real-time data from the set JVM. If the user does not want to configure the session, he can choose his favorite one from multiple IDE plugins to configure the application.

  • Remote session real-time analysis mode
    By modifying the VM parameters of the Java start command line, the user can make any Java application monitor the connection from the JProfiler GUI. The setting program can not only run on the local computer, but also can be used as an additional program of a setting program in the network. In addition, JProfiler also provides quite a few integrated wizards for mainstream application servers that can help users install and set up their applications.

  • Offline Profiling Mode
    Users can configure JProfiler GUI without connecting to the application. In offline configuration mode, users can use the powerful JProfiler trigger system or JProfiler' API to control the configuration agent, and then save the snapshot to disk. Users can later open these snapshots in the settings export view of the JProfiler GUI or programmatically using the command line export tool or the ant task export tool.

  • Snapshot comparison
    In JProfiler, users can save all the current setting data as a snapshot and store it in the disk. JProfiler provides a rich comparison function to compare the differences between two or more snapshots. Users can choose one of the programmed command-line comparison tool and the ant task comparison tool to create a comparison report.

  • View HPROF snapshots
    JProfiler can open HPROF snapshot files created with JVM tools (such as jconsole, jmap or triggered by the -XX:+HeapDumpOnOutOfMemoryError JVM parameter)

Install

IEAR download plug-in JPrifiler
insert image description here

After installing the plug-in, remember to restart IDEA. The following standard appears to indicate that the installation is successful

insert image description here

Download and install the JProfiler client from the official website of congqi. Official website download address:https://www.ej-technologies.com/download/jprofiler/files

use

generate snapshot

public class vmtest {
    
    
    public static int i=1;
    public static void main(String[] args) {
    
    
        final long max= Runtime.getRuntime().maxMemory();//jvm的视图使用的最大内存
        final long total = Runtime.getRuntime().totalMemory();//jvm初始化内存
        final int cpu = Runtime.getRuntime().availableProcessors();//cpu核数
    /*-Xms10m  -Xmx10m -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError -Xss2m
    * 表示配置jvm初始化内存为10M,最大内存为10M,打印GC详细文件,生成错误文件
    * */
        System.out.println("虚拟机获得最大内存"+(max/1024/1024)+"m");
        System.out.println("初始最大内存"+(total/1024/1024)+"m");
        System.out.println("本机核数:"+cpu);
        String s = new String();
        while(true){
    
    
           final byte[] bytes = new byte[1024 * 1024*1024];
       }
    }
}

Configure VMs

insert image description here

run the program

We expected the effect to be achieved, and the heap memory was exceeded.
insert image description here

Open the project directory to find the generated snapshot and open it with the JProfiler tool

insert image description here

Enter JProfiler

View instances in the heap

insert image description here

Check where the error is reported

insert image description here

run locally

insert image description here

Enter JProfiler

You can check the real-time data when the program is running

insert image description here

generate snapshot

Enter JProfiler

You can check the real-time data when the program is running

insert image description here

generate snapshot

insert image description here

Guess you like

Origin blog.csdn.net/u011397981/article/details/131210536