arthas

Arthas #

用于诊断java进程运行情况。

官方文档: https://arthas.aliyun.com/doc/quick-start.html

快速启动 #

bash
# 下载jar
curl -O https://arthas.aliyun.com/arthas-boot.jar
# 启动Arthas服务并进入命令行客户端
java -jar arthas-boot.jar

常用指令 #

bash
# 进入监控面板,按q或Ctrl-C退出监控面板
dashboard

# 查看静态类属性值
getstatic class_name field_name

# dump到指定文件
heapdump arthas-output/dump.hprof

# 查看内存使用情况
memory

# 查看JVM信息
jvm

# 查看环境变量
sysenv [env-name]

# 查看或修改系统属性
sysprop [property-name] [property-value]

# 列出线程信息(线程状态、CPU占用情况)
thread
# 列出WAITING状态的线程
thread --state WAITING
# 列出前3个最忙的线程
thread -n 3
# 查看指定线程id的堆栈信息
thread 123
# 找出阻塞的线程(目前只支持synchronized)
thread -b

# 查看或更新VM诊断相关的参数
vmoption [option_name] [option_value]

# 反编译
jad class_name [function_name]

# 退出当前客户端,其他客户端不受影响
quit

# 关闭服务端,所有客户端全部退出
stop

方法监控指令 #

请注意,这些命令,都通过字节码增强技术来实现的,会在指定类的方法中插入一些切面来实现数据统计和观测, 因此在线上、预发使用时,请尽量明确需要观测的类、方法以及条件,诊断结束要执行 stop 或将增强过的类执行 reset 命令。 更多指令和参考样例请查看官方文档。

样例1 #

bash
# 每隔5秒统计一次指定函数的平均耗时、成功率。
monitor -c 5 demo.MathGame primeFactors

输出:

text
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 94 ms.
 timestamp            class          method        total  success  fail  avg-rt(ms)  fail-rate
-----------------------------------------------------------------------------------------------
 2018-12-03 19:06:38  demo.MathGame  primeFactors  5      1        4     1.15        80.00%

 timestamp            class          method        total  success  fail  avg-rt(ms)  fail-rate
-----------------------------------------------------------------------------------------------
 2018-12-03 19:06:43  demo.MathGame  primeFactors  5      3        2     42.29       40.00%

样例2 #

bash
# 观察函数调用返回时的参数、this对象和返回值(属性遍历深度为2)
watch demo.MathGame primeFactors -x 2

输出:

text
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 32 ms, listenerId: 5
method=demo.MathGame.primeFactors location=AtExceptionExit
ts=2021-08-31 15:22:57; [cost=0.220625ms] result=@ArrayList[
    @Object[][
        @Integer[-179173],
    ],
    @MathGame[
        random=@Random[java.util.Random@31cefde0],
        illegalArgumentCount=@Integer[44],
    ],
    null,
]
method=demo.MathGame.primeFactors location=AtExit
ts=2021-08-31 15:22:58; [cost=1.020982ms] result=@ArrayList[
    @Object[][
        @Integer[1],
    ],
    @MathGame[
        random=@Random[java.util.Random@31cefde0],
        illegalArgumentCount=@Integer[44],
    ],
    @ArrayList[
        @Integer[2],
        @Integer[2],
        @Integer[26947],
    ],
]

样例3 #

bash
# 观察函数运行前后,当前对象中的属性。
# (可以使用target关键字,代表当前对象;使用target.field_name访问当前对象的某个属性)
watch demo.MathGame primeFactors 'target'

输出:

text
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 52 ms.
ts=2018-12-03 19:41:52; [cost=0.477882ms] result=@MathGame[
    random=@Random[java.util.Random@522b408a],
    illegalArgumentCount=@Integer[13355],
]
2025年3月23日