I have long heard that Arthas from Ali open source is very good at doing Java application diagnosis, and many colleagues around me are using it, so I decided to open a pit and learn how to use this tool from scratch. The version used in this series is the current latest version 3.4. .5.
Since Arthas has been developing for so long, its own documentation and online tutorials are very sound, and there are also third-party IDEA plug-ins and many teaching videos to help us get started. Therefore, the positioning of this series of articles is personal notes, not tutorials. , I hope not to mislead people.
Overview
https://arthas.aliyun.com
When you encounter the following similar problems and are at a loss, Arthas can help you solve them:
- Which jar package is this class loaded from? Why are various types of related Exception reported?
- Why is the code I changed not executed? Could it be that I didn't commit? Wrong branch?
- I can’t debug online if I encounter a problem, can I only republish it by adding a log?
- I encountered a user's data processing problem online, but the online debugging is also impossible, and the offline cannot be reproduced!
- Is there a global perspective to view the health of the system?
- Is there any way to monitor the real-time running status of the JVM?
- How to quickly locate application hot spots and generate flame graphs?
To use Arthas, JDK version 1.6 or above is required.
Quick install
https://arthas.aliyun.com/doc/install-detail.html
Arthas itself is also a Java process, thanks to the cross-platform features of Java, so I installed it directly on Windows.
(1) Download the Arthas package
curl -O https://arthas.aliyun.com/arthas-boot.jar
(2) Run Arthas
java -jar arthas-boot.jar
It should be noted that before running Arthas, ensure that the system is running at least one Java process, otherwise it cannot be started and an error will be reported: Can not find java process. Try to pass in command line. Please select an available pid. The solution is to run a Java application.
If you need to uninstall Arthas:
On Linux/Unix/Mac platforms, delete the following files:
rm -rf ~/.arthas/
rm -rf ~/logs/arthas
Directly delete the .arthas and logs/arthas directories under the user home on the Windows platform
Quick start
4.1 attach process
Here we use the demo package officially provided by Arthas, so we don't need to write code by ourselves. Download the demo package and run it.
curl -O https://arthas.aliyun.com/arthas-demo.jar
java -jar arthas-demo.jar
This demo function is to perform prime factor decomposition in an endless loop, and record the number of times that cannot be decomposed, as shown in the figure below.
We first start Arthas and attach the process.
By default, Arthas only listens to 127.0.0.1, so if you want to connect remotely, you can use the --target-ip parameter to specify the listener's IP
In addition, if conditions permit, you can also log in with a browser after attaching, and visit: http://127.0.0.1:3658. You can also fill in the IP to connect to Arthas of other machines remotely.
4.2 Commonly used commands
4.2.1 dashboard
https://arthas.aliyun.com/doc/dashboard.html
Use the dastboard command to view the Java process information (regular refresh), if you need to exit, use q. It consists of the following four parts:
- The first part is to display all threads running in the JVM: the thread group, priority, thread status, CPU occupancy, whether it is a background process, etc.
- The second part shows the usage of JVM memory
- The third part shows GC related information
- The fourth part is some information about the operating system and the Java version number
4.2.2 thread
https://arthas.aliyun.com/doc/thread.html
Use the thread command to view all current thread information.
And you can view the status of a specific thread by appending PID.
4.2.3 jad
https://arthas.aliyun.com/doc/jad.html
Use the jad command to decompile the class file.
4.2.2 watch
https://arthas.aliyun.com/doc/watch.html
The watch command can monitor the input and output parameters of a method:
Exit Arthas
If you just exit the current connection, you can use the quit or exit command. The Arthas attached to the target process will continue to run, the port will remain open, and it can be directly connected the next time it is connected.
If you want to completely exit arthas, you can execute the stop command.