SoFunction
Updated on 2025-04-09

Quick Start and Practice of Java Diagnostic Tool Arthas

introduction

In Java development, we often encounter various performance problems, memory leaks, thread blocking and other problems. These problems are often difficult to locate and resolve through conventional logging and monitoring tools. As an open source Java diagnostic tool, Arthas provides powerful real-time monitoring and diagnostic functions, which can help developers quickly locate and solve these problems. This article will introduce in detail the installation, basic use of Arthas and some commonly used commands to help readers quickly get started and apply it to actual development.

1. Introduction to Arthas

Arthas is an open source Java diagnostic tool from Alibaba, which supports JDK 6+, and can monitor and diagnose the running status of Java applications in real time without restarting the application. Arthas provides a rich set of commands that can help developers quickly locate performance bottlenecks, memory leaks, thread blocking and other problems.

1.1 The main functions of Arthas

  • Real-time monitoring: You can view the running status of the JVM in real time, including threads, memory, GC and other information.
  • Dynamic tracking: You can dynamically track the call status of the method without modifying the code.
  • Hot updates: The bytecode of the class can be dynamically modified without restarting the application.
  • Decompile: You can decompile the loaded class and view its source code.
  • Performance Analysis: It can analyze performance indicators such as the execution time and number of calls of the method.

2. Install Arthas

2.1 Download Arthas

The installation of Arthas is very simple, you just need to download its startup script. You can download it through the following command:

curl -O /

2.2 Start Arthas

After the download is complete, you can start Arthas with the following command:

java -jar 

After startup, Arthas will list all Java processes in the current system, and users can select the processes that need to be diagnosed to connect.

$ java -jar 
* [1]: 35542
  [2]: 71560 

Select the corresponding process number and Arthas will connect to the target process and start a diagnostic session.

3. Quick Start

3.1 Start the sample application

To better demonstrate the use of Arthas, we can start a simple Java applicationmath-game. This application generates a random number every second and performs prime factorization on it.

curl -O /
java -jar 

3.2 View Dashboard

After the connection is successful, you can passdashboardThe command checks the running status of the current process, including thread, memory, GC and other information.

$ dashboard
ID     NAME                   GROUP          PRIORI STATE  %CPU    TIME   INTERRU DAEMON
17     pool-2-thread-1        system         5      WAITIN 67      0:0    false   false
27     Timer-for-arthas-dashb system         10     RUNNAB 32      0:0    false   true
...

3.3 Get Main Class

passthreadThe command can be obtainedmath-gameMain Class of the process.

$ thread 1 | grep 'main('
    at (:17)

3.4 Decompile Main Class

passjadThe command can decompile Main Class and view its source code.

$ jad 

ClassLoader:
+-$AppClassLoader@3d4eac69
  +-$ExtClassLoader@66350f69

Location:
/tmp/

/*
 * Decompiled with CFR 0_132.
 */
package demo;

import ;
import ;
import ;
import ;
import ;
import ;

public class MathGame {
    private static Random random = new Random();
    private int illegalArgumentCount = 0;

    public static void main(String[] args) throws InterruptedException {
        MathGame game = new MathGame();
        do {
            ();
            (1L);
        } while (true);
    }

    public void run() throws InterruptedException {
        try {
            int number = ();
            List<Integer> primeFactors = (number);
            (number, primeFactors);
        }
        catch (Exception e) {
            (("illegalArgumentCount:%3d, ", ) + ());
        }
    }

    public static void print(int number, List<Integer> primeFactors) {
        StringBuffer sb = new StringBuffer("" + number + "=");
        Iterator<Integer> iterator = ();
        while (()) {
            int factor = ();
            (factor).append('*');
        }
        if ((() - 1) == '*') {
            (() - 1);
        }
        (sb);
    }

    public List<Integer> primeFactors(int number) {
        if (number < 2) {
            ++;
            throw new IllegalArgumentException("number is: " + number + ", need >= 2");
        }
        ArrayList<Integer> result = new ArrayList<Integer>();
        int i = 2;
        while (i <= number) {
            if (number % i == 0) {
                (i);
                number /= i;
                i = 2;
                continue;
            }
            ++i;
        }
        return result;
    }
}

3.5 Monitoring method returns value

passwatchCommands can be monitored#primeFactorsThe return value of the method.

$ watch  primeFactors returnObj
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 107 ms.
ts=2018-11-28 19:22:30; [cost=1.715367ms] result=null
ts=2018-11-28 19:22:31; [cost=0.185203ms] result=null
ts=2018-11-28 19:22:32; [cost=19.012416ms] result=@ArrayList[
    @Integer[5],
    @Integer[47],
    @Integer[2675531],
]
...

3.6 Exit Arthas

If you just exit the current connection, you can usequitorexitOrder. If you want to exit Arthas completely, you can execute itstopOrder.

$ stop

4. Advanced use

4.1 Dynamic tracking method calls

Arthas providestraceCommands can dynamically track the call status of the method and help developers analyze the execution time and number of calls of the method.

$ trace  run
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 107 ms.
ts=2018-11-28 19:22:30; [cost=1.715367ms] result=null
ts=2018-11-28 19:22:31; [cost=0.185203ms] result=null
ts=2018-11-28 19:22:32; [cost=19.012416ms] result=@ArrayList[
    @Integer[5],
    @Integer[47],
    @Integer[2675531],
]
...

4.2 Hot update code

Arthas supports dynamically modifying the bytecode of the class without restarting the application. passredefineCommands that reload the modified class into the JVM.

$ redefine /path/to/new/

4.3 Performance Analysis

passprofilerCommands can analyze performance of Java applications and generate flame diagrams to help developers quickly locate performance bottlenecks.

$ profiler start
$ profiler stop

5. Summary

As a powerful Java diagnostic tool, Arthas provides a rich set of commands and real-time monitoring functions, which can help developers quickly locate and solve various problems in Java applications. Through the introduction of this article, readers can quickly get started with Arthas and apply it to actual development. Whether it is performance analysis, memory leak troubleshooting, or dynamic tracking method calls, Arthas provides powerful support.

The above is the detailed content of the quick introduction and practice of Java diagnostic tool Arthas. For more information about Java diagnostic tool Arthas, please follow my other related articles!