Installation and use of Arthas in mac system

Arthas is a Java diagnostic tool open source by Alibaba in September 2018. It supports JDK6 + and adopts command-line interaction mode, which can easily locate and diagnose online program operation problems. The official document of Arthas is very detailed. See:

Arthas usage scenario

Thanks to the powerful and rich functions of Arthas, what Arthas can do is beyond imagination. Just a few common usage cases are listed below. More usage scenarios can be explored by yourself after you are familiar with Arthas.
Is there a global perspective to view the health of the system?
Why does the CPU rise again? Where does it occupy the CPU?
Is there a deadlock in the running multithread? Is there a blockage?
The program takes a long time to run. Where does it take a long time? How to monitor?
Which jar package is this class loaded from? Why do you report all kinds of related exceptions?
Why didn't the code I changed execute? Am I not commit ted? Wrong branch?
You can't debug online when you encounter problems. Can you only republish it by adding logs?
Is there any way to monitor the real-time running status of the JVM?

1, Download Arthas boot jar

There are two official ways to download Arthas boot Jar or directly install and use as sh. The former is explained here. I download the jar package

 curl -O
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  138k  100  138k    0     0   177k      0 --:--:-- --:--:-- --:--:--  176k

2, Run Arthas boot jar

The whole arthas is developed in java language, so it can be run directly in java jar

$ java -jar arthas-boot.jar
[INFO] arthas-boot version: 3.5.3
[INFO] Process 67746 already using port 3658
[INFO] Process 67746 already using port 8563
[INFO] Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER.
* [1]: 67746 springboot-mybatis-all-1.0.0.jar
  [2]: 70997 com.huohuo.service.jvm.DeadLockTest
  [3]: 70996 org.jetbrains.jps.cmdline.Launcher
  [4]: 70570
  [5]: 70798 org.jetbrains.idea.maven.server.RemoteMavenServer36
[INFO] arthas home: /Users/huoyajing/.arthas/lib/3.5.5/arthas
[INFO] The target process already listen port 3658, skip attach.
[INFO] arthas-client connect 3658
  ,---.  ,------. ,--------.,--.  ,--.  ,---.   ,---.
 /  O  \ |  .--. ''--.  .--'|  '--'  | /  O  \ '   .-'
|  .-.  ||  '--'.'   |  |   |  .--.  ||  .-.  |`.  `-.
|  | |  ||  |\  \    |  |   |  |  |  ||  | |  |.-'    |
`--' `--'`--' '--'   `--'   `--'  `--'`--' `--'`-----'

version    3.5.5
main_class org.springframework.boot.loader.JarLauncher
pid        67746
time       2022-03-02 15:43:29


1) Select the jps process to monitor

You can also directly Java - jar Arthas boot Jar PID directly into the topic.

Note:[ arthas@67746 ]$indicates that you have opened the monitoring client, where you can execute relevant commands for viewing. You can use quit (exit the current client), stop\shutdown (close the Arthas server and exit all clients).
67746 is PID.

2) Visual interface operation

In addition to viewing on the command line, it can also be accessed through the browser see. As shown in the following figure: you can directly operate the relevant command parameters

3, Common commands


[arthas@73120]$ help
 help         Display Arthas Help
 auth         Authenticates the current session
 keymap       Display all the available keymap for the specified connection.
 sc           Search all the classes loaded by JVM
 sm           Search the method of classes loaded by JVM
 classloader  Show classloader info
 jad          Decompile class
 getstatic    Show the static field of a class
 monitor      Monitor method execution statistics, e.g. total/success/failure c
              ount, average rt, fail rate, etc.
 stack        Display the stack trace for the specified class and method
 thread       Display thread info, thread stack
 trace        Trace the execution time of specified method invocation.
 watch        Display the input/output parameter, return object, and thrown exc
              eption of specified method invocation
 tt           Time Tunnel
 jvm          Display the target JVM information
 perfcounter  Display the perf counter information.
 ognl         Execute ognl expression.
 mc           Memory compiler, compiles java files into bytecode and class file
              s in memory.
 redefine     Redefine classes. @see Instrumentation#redefineClasses(ClassDefin
 retransform  Retransform classes. @see Instrumentation#retransformClasses(Clas
 dashboard    Overview of target jvm's thread, memory, gc, vm, tomcat info.
 dump         Dump class byte array from JVM
 heapdump     Heap dump
 options      View and change various Arthas options
 cls          Clear the screen
 reset        Reset all the enhanced classes
 version      Display Arthas version
 session      Display current session information
 sysprop      Display, and change the system properties.
 sysenv       Display the system env.
 vmoption     Display, and update the vm diagnostic options.
 logger       Print logger info, and update the logger level
 history      Display command history
 cat          Concatenate and print files
 base64       Encode and decode using Base64 representation
 echo         write arguments to the standard output
 pwd          Return working directory name
 mbean        Display the mbean information
 grep         grep command for pipes.
 tee          tee command for pipes.
 profiler     Async Profiler.
 vmtool       jvm tool
 stop         Stop/Shutdown Arthas server and exit the console.

2) dashboard can view the running status of the whole process, including thread, memory, GC and running environment information

It mainly shows three parts: thread, jvm and Java env. It will refresh automatically every few seconds

3) Thread thread information

In fact, it is the first part of the result obtained by dashboard

thread -n num view the results of the first three threads and sort them according to CPU usage

[arthas@16645]$ thread -n 3
"Thread-0" Id=10 cpuUsage=99.16% deltaTime=199ms time=53423ms RUNNABLE
    at com.huohuo.service.jvm.ArthasTest.lambda$cpuHigh$1(
    at com.huohuo.service.jvm.ArthasTest$$Lambda$1/ Source)

"arthas-command-execute" Id=28 cpuUsage=0.16% deltaTime=0ms time=18ms RUNNABLE
    at Method)
    at java.util.concurrent.Executors$
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(
    at java.util.concurrent.ScheduledThreadPoolExecutor$
    at java.util.concurrent.ThreadPoolExecutor.runWorker(
    at java.util.concurrent.ThreadPoolExecutor$

"C1 CompilerThread2" [Internal] cpuUsage=0.13% deltaTime=0ms time=462ms

4) thread pid view the specific result of a thread

5) thread -b deadlock information without pid

[arthas@3987]$ thread -b
"Thread-1" Id=11 BLOCKED on java.lang.Object@57d20628 owned by "Thread-2" Id=12
    at com.huohuo.service.jvm.ArthasTest.lambda$deadThread$2(
    -  blocked on java.lang.Object@57d20628
    -  locked java.lang.Object@7bd50b57 <---- but blocks 1 other threads!
    at com.huohuo.service.jvm.ArthasTest$$Lambda$2/ Source)
[arthas@3987]$ thread 10
"Thread-0" Id=10 RUNNABLE
    at com.huohuo.service.jvm.ArthasTest.lambda$cpuHigh$1(
    at com.huohuo.service.jvm.ArthasTest$$Lambda$1/ Source)

We can clearly see the problem arthastest Java: there is a problem with line 44.

6) Full path decompilation of jad class

This makes it easy for us to check whether the online code is the correct version.

[arthas@3987]$ jad com.huohuo.service.jvm.ArthasTest

//These are the class loading mechanisms AppClassLoader (application class loader) and ExtClassLoader (extension class loader)

//My project path

//The following information is clear. It's very convenient to check whether the online code is the latest. It's very convenient

        * Decompiled with CFR.
       package com.huohuo.service.jvm;

       import java.util.HashSet;

       public class ArthasTest {
           private static HashSet hashSet = new HashSet();

           public static void cpuHigh() {
               new Thread(() -> {
                   while (true) {
                       // Infinite loop

           public static void addHashSetThread() {
               new Thread(() -> {
/*29*/             int count = 0;
                   while (true) {
                       try {
                           while (true) {


7) ognl can view / modify the values on the system line

ognl '@ full path category @ static attribute name' calls static attribute

[arthas@11021]$ ognl @com.huohuo.service.jvm.ArthasTest@hashSet
[arthas@11021]$ ognl @com.huohuo.service.jvm.ArthasTest@integer

ognl '@ full path category @ static method name ("parameter")' call static method

ognl '@com.huohuo.service.jvm.ArthasTest@hashSet.add("huohuo")'


8) sc -d full path class information

[arthas@16645]$ sc -d com.huohuo.service.jvm.ArthasTest
 class-info        com.huohuo.service.jvm.ArthasTest
 code-source       /Users/huoyajing/information/spring/springBoot/J2020/springbo
 name              com.huohuo.service.jvm.ArthasTest
 isInterface       false
 isAnnotation      false
 isEnum            false
 isAnonymousClass  false
 isArray           false
 isLocalClass      false
 isMemberClass     false
 isPrimitive       false
 isSynthetic       false
 simple-name       ArthasTest
 modifier          public
 super-class       +-java.lang.Object
 class-loader      +-sun.misc.Launcher$AppClassLoader@18b4aac2
 classLoaderHash   18b4aac2

Affect(row-cnt:1) cost in 33 ms.

For more commands, click

4, [ERROR] attach fail, targetPid: 77710

I started the java process directly from idea and run.

$ java -jar arthas-boot.jar
[INFO] arthas-boot version: 3.5.1
[INFO] Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER.
* [1]: 70570
  [2]: 77709 org.jetbrains.jps.cmdline.Launcher
  [3]: 77710 com.huohuo.service.jvm.ArthasTest
[INFO] arthas home: /Users/huoyajing/.arthas/lib/3.5.5/arthas
[INFO] Try to attach process 77710
[WARN] Current VM java version: 13 do not match target VM java version: 1.8, attach may fail.
[WARN] Target VM JAVA_HOME is /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre, arthas-boot JAVA_HOME is /Library/Java/JavaVirtualMachines/jdk-13.0.2.jdk/Contents/Home, try to set the same JAVA_HOME.
[ERROR] Start arthas failed, exception stack trace: 0
	at jdk.attach/
	at jdk.attach/
	at jdk.attach/
[ERROR] attach fail, targetPid: 77710

$ java -version
java version "13.0.2" 2020-01-14
Java(TM) SE Runtime Environment (build 13.0.2+8)
Java HotSpot(TM) 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)

Then I switched the JDK version to JDK8. It's really no problem. Why is this problem? Because my Idea is executed using JDK8. iTerm started with JDK13, so the version is different. Change it to solve the problem.

Keywords: Java jvm arthas

Added by spicey on Wed, 02 Mar 2022 16:02:32 +0200