Arthas简单使用指南
目录
一、Arthas下载
二、启动一个Java应用
三、启动Arthas监控Java应用
四、Arthas常用命令
1、系统状态监控
1.1、dashboard
1.2、jvm
2、线程监控
2.1、thread
2.2、thread thread-id
2.3、thread -n
2.4、thread -b
3、查看类信息
3.1、sc
3.2、sm
3.3、jad
4、方法监控
4.1、watch
4.2、trace
5、内存监控
5.1、heapdump
5.2、monitor
6、系统命令
6.1、sysprop
6.2、sysenv
7、退出命令
7.1、quit
7.2、stop
一、Arthas下载
wget https://arthas.aliyun.com/arthas-boot.jar
curl -O https://arthas.aliyun.com/arthas-boot.jar
二、启动一个Java应用
自己新建一个Java应用,或使用arthas提供demo,
下载:wget https://arthas.aliyun.com/arthas-demo.jar
运行:java -jar arthas-demo.jar
查看pid: ps -ef | grep java
pid是77348
三、启动Arthas监控Java应用
Arthas启动:java -jar arthas-boot.jar 77348
四、Arthas常用命令
1、系统状态监控
1.1、dashboard
实时展示 JVM 的关键指标,如堆内存、线程、类加载等
快速查看系统的整体健康状况。
输入Q 后回车 或 Ctrl+C 退出dashboard命令。
1.2、jvm
查看 JVM 的详细信息,包括系统属性、环境变量等。
排查 JVM 配置问题,如内存泄漏、线程过多等。

2、线程监控
2.1、thread
查看当前线程的状态,包括线程的 CPU 使用率、线程堆栈信息等。
排查线程阻塞、线程泄漏等问题。

2.2、thread thread-id
thread <thread-id>
查看线程的详细信息

2.3、thread -n <number>
thread -n <number>
通过指定 -n 参数,可以查看 CPU 使用率最高的前 number个线程。
快速定位 CPU 使用率高的线程。

2.4、thread -b
thread -b
查看阻塞线程。
排查线程阻塞问题。
![]()
3、查看类信息
3.1、sc
查看类的详细信息,包括类名、包名、加载器等。
排查类加载问题,如类是否被加载。
sc
这个命令会列出当前 JVM 中所有已加载的类。
sc <class-pattern>
通过指定类名,可以查看该类的详细加载信息,包括类加载器、类路径等。
<class-pattern>:类的全限定名,支持通配符(* 和 ?)。例如:
com.example.*:匹配 com.example 包下的所有类。
com.example.service.*Service:匹配 com.example.service 包下所有以 Service 结尾的类。
*:匹配所有类。
-f:显示类的加载器信息。
-d:显示类的加载器的详细信息。
-b:显示类的加载器的哈希值。
-s:显示类的大小(字节)。
-c:显示类的加载器的类名。
-n:显示类的加载器的名称。
-l:显示类的加载器的层次结构

3.2、sm
查看类的方法信息,包括方法的签名、修饰符、注解
查看类中定义了哪些方法。
sm <class-pattern> <method-pattern>
<class-pattern>:类的全限定名,支持通配符(* 和 ?)。例如:
com.example.*:匹配 com.example 包下的所有类。
com.example.service.*Service:匹配 com.example.service 包下所有以 Service 结尾的类。
<method-pattern>:方法名,支持通配符(* 和 ?)。例如:
*:匹配所有方法。
get*:匹配所有以 get 开头的方法。
-d:显示方法的详细信息,包括注解、参数类型、返回类型等。
-f:显示方法的类加载器信息。
-c:显示方法的类加载器的类名。
-n:显示方法的类加载器的名称。
-l:显示方法的类加载器的层次结构。
3.3、jad
反编译类文件。
看类的源码,帮助理解代码逻辑。
jad <class-name>
<class-name>:目标类的全限定名,例如 com.example.MyClass。

4、方法监控
4.1、watch
监控方法的执行情况,包括方法的入参、返回值、异常等。
排查方法的输入输出问题,如参数异常、返回值异常等。
watch <class-name> <method-name> <expression>
<class-name>:目标类的全限定名,例如 com.example.MyClass。
<method-name>:目标方法的名称,例如 myMethod。
<expression>:要监控的表达式,可以是方法的入参、出参、执行时间等。
<expression>包含:
-
params -
描述:方法的入参。
-
示例:
params或params[0](访问第一个参数)。 -
returnObj -
描述:方法的返回值。
-
示例:
returnObj。 -
throwable -
描述:方法抛出的异常。
-
示例:
throwable。 -
@cost -
描述:方法的执行时间(单位:毫秒)。
-
示例:
@cost。 -
target -
描述:被调用方法的对象实例。
-
示例:
target。 -
self -
描述:当前执行的类实例。
-
示例:
self。 -
joinPoint -
描述:当前执行的连接点信息。
-
示例:
joinPoint。

4.2、trace
跟踪方法的执行路径,查看方法的调用链路。
排查方法调用链路中的问题,如性能瓶颈。
追踪方法的调用链路
trace <class-name> <method-name>
<class-name>:目标类的全限定名,例如 com.example.MyClass。
<method-name>:目标方法的名称,例如 myMethod。

指定参数类型
如果目标方法有多个重载版本,可以通过指定参数类型来选择特定的重载方法:
trace <class-name> <method-name> -p <param-types>
<param-types>:方法的参数类型,用逗号分隔,例如 java.lang.String,int。

指定表达式
trace <class-name> <method-name> <expression>
你可以通过 -e 参数指定要追踪的表达式,例如方法的入参、出参、执行时间等:
运行:
watch com.zwf.arthas.controller.UserController addUser "{params, returnObj}"
没有监控到
5、内存监控
5.1、heapdump
生成 JVM 的堆转储文件,用于后续分析。
排查内存泄漏问题。
heapdump –live 50 /path/to/heapdump.hprof
--live:指定存活对象的比例(例如 50 表示只包含存活对象的 50%)。
/path/to/heapdump.hprof:指定堆转储文件的保存路径。
heapdump /path/to/heapdump.hprof
生成完整的堆转储

5.2、monitor
用于监控方法的执行情况,包括方法的调用次数、执行时间等
monitor -c 10 -n 5 com.zwf.arthas.controller.UserController addUser
-c 10:监控 10 秒。
-n 5:每 5 秒输出一次统计信息。
com.zwf.arthas.controller.UserController:目标类的全限定名。
addUser:目标方法的名称
参数说明
-c:监控的总时长(单位:秒)。
-n:统计信息的输出间隔(单位:秒)。
<class-name>:目标类的全限定名。
<method-name>:目标方法的名称。

6、系统命令
6.1、sysprop
查看系统信息,包括操作系统、CPU、内存等。
快速了解系统的基本信息。
sysprop

查看特定系统属性
sysprop <property-name>
<property-name>:指定要查看的系统属性名称。

设置系统属性
sysprop <property-name> <property-value>
<property-name>:指定要设置的系统属性名称。
<property-value>:指定要设置的系统属性值。

6.2、sysenv
查看系统环境变量。
sysenv
查看所有

sysenv <variable-name>
<variable-name>:指定要查看的环境变量名称。
查看指定

7、退出命令
7.1、quit
退出 Arthas。
退出 Arthas,结束监控。

7.2、stop
停止 Arthas 会话。
停止当前 Arthas 会话,释放资源。

作者:zwf-leaves