[jvm series-12] jvm performance tuning --- basic use of GUI tools

JVM series overall column


content link address
[1] Getting to know virtual machines and java virtual machines https://blog.csdn.net/zhenghuishengq/article/details/129544460
[2] The class loading subsystem of jvm and the basic use of jclasslib https://blog.csdn.net/zhenghuishengq/article/details/129610963
[3] The virtual machine stack, program counter, and local method stack of the private area at runtime https://blog.csdn.net/zhenghuishengq/article/details/129684076
[4] Heap and escape analysis of the shared area of ​​the data area at runtime https://blog.csdn.net/zhenghuishengq/article/details/129796509
[5] The method area and constant pool of the runtime data area shared area https://blog.csdn.net/zhenghuishengq/article/details/129958466
[6] Object instantiation, memory layout and access positioning https://blog.csdn.net/zhenghuishengq/article/details/130057210
[7] Execution engine, interpreter, JIT instant compiler https://blog.csdn.net/zhenghuishengq/article/details/130088553
[8] Proficient in the underlying mechanism of String https://blog.csdn.net/zhenghuishengq/article/details/130154453
[9] The underlying principles and algorithms of garbage collection and the basic use of JProfiler https://blog.csdn.net/zhenghuishengq/article/details/130261481
[10] Types of garbage collectors and internal execution principles https://blog.csdn.net/zhenghuishengq/article/details/130261481
[11] Basic use of command line tools for jvm performance tuning https://blog.csdn.net/zhenghuishengq/article/details/130641456
[12] Basic use of GUI tools for jvm performance tuning https://blog.csdn.net/zhenghuishengq/article/details/130745931

One, JVM diagnostic monitoring and the use of GUI tools

1. Overview of GUI tools

Command-line tools or combinations can obtain basic information related to Java application performance, but there are certain limitations, such as the inability to obtain method-level data analysis, call relationships between methods, number of calls and call time, etc. , and when using the command line tool, you need to log in to the virtual machine where the Java application is located, which is not very convenient to use, and finally display the data output through the terminal, which is not intuitive enough. Therefore, in order to solve the problems caused by command line tools, GUI tools were born.

Among the graphical tools, there are mainly JDK built-in tools and some third-party tools. The JDK built-in tools mainly include jConsole, visual VM, JMC, etc., and the third-party tools mainly include MAT, JProfiler, Arthas, and Btrace . jconsole and visual VM must be understood and mastered. JProfiler and Arthas belong to the third party. Compared with the former two, the functions of the third-party tools will be more and more powerful

2,jConsole

jconsole is used to monitor jvm memory, threads and classes, and is a JMX-based GUI performance monitoring tool. You can find this jconsole.exe under the bin directory under the jdk installation directory and double-click to start it. You can also directly enter jconsole in the cmd command to open this tool. The opened interface is as follows

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-KTw6F8XE-1684390467232)(img/1684115452965.png)]

There are three connection methods of jconsole, which are Local local connection, Remote remote connection and Advanced connection. If you use remote connection, you need to go through a JMX proxy, and set the corresponding user name and password to authorize. What needs to be noted when using a local connection is that the user who starts the local code and the user who starts jconsole must be the same user.

Here we take the local connection as an example, just run a piece of code, and then enter jconsole in the cmd command line tool, and then you can find its process number in the local process without using jps, and then double-click into this process, you can find the following The interface, as can be seen from the figure below, in this interface, the usage of heap memory, threads, classes, and cpu usage can be monitored in real time.

insert image description here

For example, the monitoring status of its memory is as follows, the information of heap memory and non-heap memory will be displayed, and the heap memory will be divided into Eden area and Survivor area, and the memory will be monitored every second. The broken line graph is as follows, representing the memory Reaching a certain amount will trigger a GC

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-Rg7fDaLj-1684390467234)(img/1684116612304.png)]

At the same time, the thread is also monitored every second, and there is also the function of monitoring deadlock, and the GUI corresponding to jstack

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-Dmnw1IAt-1684390467234)(img/1684116848651.png)]

And you can know the name of the garbage collector used by the new generation and the old generation, the operating system, the jvm supplier and other information through the VM summary

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-LyuP0BeL-1684390467235)(img/1684116275018.png)]

3. Visual Vm (emphasis)

Visual VM is a powerful all-in-one visual tool for troubleshooting and performance monitoring. It integrates multiple JDK command line tools. It can display virtual machine process configuration and environment information (jps, jinfo) and monitor applications. The program's CPU, GC, heap, method area, and thread information (jstack, jstat), etc., can even replace the use of jConsole.

Visual VM is not only powerful in its own right, but also supports the expansion of plug-ins, and the installation of plug-ins is also very convenient. Check out this plug-in of Visual GC as installed before. To open this Visual VM, you only need to find the bin directory under the jdk installation directory, find this JVisual VM and double-click to open it.

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-HtW2OKQy-1684390467236)(img/1684137718101.png)]

It is mainly divided into remote monitoring and local monitoring. Remote monitoring is not considered here for the time being. It is mainly local monitoring. All jvm processes will be monitored here.

You can directly right-click on the process to generate a snapshot file of the heap dump and a snapshot file of the thread. The snapshot file is similar to a screenshot at that moment. You can view stack information, deadlock issues, etc. by viewing this snapshot file.

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-2swiJRS8-1684390467243)(img/1684199561538.png)]

In addition to the stack, the CPU and memory can also be analyzed, and corresponding snapshots can also be generated.

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-osklAfcT-1684390467243)(img/1684199936450.png)]

4, JProfiler (strong)

If you want to test the memory usage while running java, you need to use a test tool. There is a MAT plug-in in eclipse that can be tested, and there is also a plug-in in idea, which is JProfiler. Compared with Visual VM, JProfiler is a relatively more powerful tool. It is a tool that does not come with JDK, so you need to pay to use it.

JProfiler has powerful functions and friendly operation. It has little impact on the analyzed application, and has powerful analysis functions for CPU, Thread, and Memory. It also supports jdbc, nosql, jsp, serverlet, socket, etc., and also supports local monitoring and monitoring. remote operation.

Through this JProfiler, you can have the following benefits:

  • Analyze what the program is doing on method calls and find ways to improve its performance;
  • In terms of memory allocation, memory leaks are fixed by analyzing heap objects, reference chains, etc., so as to optimize memory usage;
  • On threads and locks, more related views can be provided to analyze and discover multi-thread problems;
  • On the high-level subsystem, it supports integrated analysis of the heap subsystem, such as integrating JDBC and finding out the slowest execution statement.

In the ninth article of this series, I explained the installation tutorial of this JProfiler. After installing this plug-in, the idea will be displayed on the idea. https://blog.csdn.net/zhenghuishengq/article/details/130261481

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-QNpbln64-1684390467244)(img/1684217634865.png)]

I installed version 11 here, and its activation code is:S-J11-Everyone#553890-77gnvb3w8t9rd#4735

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-IIzNIfQ8-1684390467245)(img/1684218400742.png)]

After installing the idea plug-in and local installation, you need to integrate idea in this session and select the corresponding installed idea.

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-yBXhURUk-1684390467246)(img/1684219328195.png)]

After the installation is complete, click the JProfiler button, the following interface will appear, and the console will print some JProfiler things, and then click ok directly.

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-WEnDqnGR-1684390467246)(img/1684219916060.png)]

Then the following screen will appear, which will monitor the heap, CPU, thread, memory, total number of classes, time point and number of GC in real time, etc.

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-ICnh54Gq-1684390467247)(img/1684220676401.png)]

Next, simulate the previous deadlock program

/**
 * @author zhenghuisheng
 * @date : 2023/5/12
 */
public class DeadBlockTest {
    
    
    public static void main(String[] args) {
    
    
        StringBuilder s1 = new StringBuilder();
        StringBuilder s2= new StringBuilder();
        new Thread(){
    
    
            @Override
            public void run() {
    
    
                synchronized (s1){
    
    
                    s1.append("a");
                    s1.append("b");
                    try {
    
    
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
    
    
                        e.printStackTrace();
                    }
                    synchronized (s2){
    
    
                        s2.append("c");
                        s2.append("d");
                    }
                }
            }
        }.start();

        new Thread(){
    
    
            @Override
            public void run() {
    
    
                synchronized (s2){
    
    
                    s1.append("a");
                    s1.append("b");
                    try {
    
    
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
    
    
                        e.printStackTrace();
                    }
                    synchronized (s1){
    
    
                        s2.append("c");
                        s2.append("d");
                    }
                }
            }
        }.start();
    }
}

The result is as follows, it will directly inform that there are two thread deadlocks (really strong this thing)

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-U89wAquT-1684390467247)(img/1684221112359.png)]

There are many functions in it, such as viewing memory leaks through graphs and charts, etc., you can install JProfiler to test it yourself.

5,Arthas

The above tools are enough for daily development. Next, we will talk about Arthas, an open source development tool of Ali. The modification tool can monitor these data without remote connection or configuration of monitoring parameters. Its official documents are as follows: arthas.aliyun.com/zh-cn/

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-doShtNhl-1684390467248)(img/1684389172983.png)]

The problems that Arthas can solve are as follows:

  • Which jar package is the class loaded from? Why is this exception reported? When encountering problems online, can only add logs to restart?
  • There is a problem with online user data processing, which cannot be reproduced offline, how to solve it
  • Is there a global perspective to see the health of the system
  • Is there any way to monitor the real-time running status of the JVM
  • How to quickly locate application hotspots and generate flame graphs

Its installation method can directly enter this address in the browser: https://alibaba.github.io/arthas/arthas-boot.jar , wait for a while to download the jar package, and then transfer the downloaded jar package to Linux on the server, and then java -jar arthas-boot.jarrun directly

The relevant instructions on the basis of arthas are as follows:

help: 查看帮助的指令
cat:  打印文件内容,和linux中的目录一样
echo: 打印参数,和linux里的tee相似
pwd:  返回当前目录
session: 查看当前回话信息
history: 打印历史命令

The instructions related to jvm are as follows:

dashboard:  当前系统的实时数据面板,可以加打印的总次数(-n)和间隔参数(-i)
thread:    查看当前JVM的线程堆栈信息
jvm:        查看当前JVM的信息
sysprop:   查看和修改JVM的系统属性
sysenv:    查看JVM的环境变量
getstatic: 查看类的静态属性
heapdump:  生成堆快照

Information related to class and classLoader

sc:          查看JVM已加载类的信息、
sm:         查看已加载类的方法的信息
jad:        反编译已加载类的源码
mc:         .java编译成.class
redifine:   加载外部的.class文件
classloader:查看类的加载器信息

Information related to method instructions

monitor:     方法执行监控,如调用次数,执行时间,失败率等
watch:       方法执行数据监测,如异常,返回值,入参等
trace:      方法内部调用路径,输出方法上面每个结点的耗时
stack:      输出当前方法被调用的调用路径

6. Summary

In daily development, since my company uses a windows server online, the interface is relatively friendly. I prefer to use this JProfiler tool during project operation to check problems such as deadlocks and memory leaks. Of course, Arthas is relatively more suitable. Used on Linux servers.

In addition to these visual interfaces, there are also JMC, Flame Graphs, Btrace, YourKit, Jprobe, Spring Insight, etc., which are all available.

All in all, there is no best tool, only the most suitable development tool for your own use. Mastering Jconsole, JVisual VM, and JProfiler can basically solve many problems. If there are many intractable diseases online, you can master another Arthas .

Guess you like

Origin blog.csdn.net/zhenghuishengq/article/details/130745931