Java诊断工具Arthas的简单使用

Arthas简介:

Arthas是Alibaba开源的Java诊断工具。它可以协助解决以下问题:

  1. 这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception?

  2. 我改的代码为什么没有执行到?难道是我没 commit?分支搞错了?

  3. 遇到问题无法在线上 debug,难道只能通过加日志再重新发布吗?

  4. 线上遇到某个用户的数据处理有问题,但线上同样无法 debug,线下无法重现!

  5. 是否有一个全局视角来查看系统的运行状况?

  6. 有什么办法可以监控到JVM的实时运行状态?

  7. 怎么快速定位应用的热点,生成火焰图?

  8. 怎样直接从JVM内查找某个类的实例?

我一般用来找出高并发场景中,影响性能的瓶颈所在,所以这里简单介绍一下如何使用Arthas找出系统性能瓶颈。

Arthas简单使用

1.下载Arthas

curl -O https://arthas.aliyun.com/arthas-boot.jar

2.先启动被诊断的进程,然后运行Arthas

java -jar arthas-boot.jar

运行后会显示正在运行的java程序,每个进程都有编号,如:

[INFO] arthas-boot version: 3.5.4
[INFO] Process 38220 already using port 3658
[INFO] Process 38220 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]: 38220 study-0.0.1-SNAPSHOT.jar

3.输入被诊断的进程前面的序号,然后回车,等输出类似如下的日志以后,就可以开始使用Arthas的命令了

[INFO] arthas home: C:\Users\java_t_t\.arthas\lib\3.5.4\arthas
[INFO] The target process already listen port 3658, skip attach.
[INFO] arthas-client connect 127.0.0.1 3658
  ,---.  ,------. ,--------.,--.  ,--.  ,---.   ,---.
 /  O  \ |  .--. ''--.  .--'|  '--'  | /  O  \ '   .-'
|  .-.  ||  '--'.'   |  |   |  .--.  ||  .-.  |`.  `-.
|  | |  ||  |\  \    |  |   |  |  |  ||  | |  |.-'    |
`--' `--'`--' '--'   `--'   `--'  `--'`--' `--'`-----'
​
​
wiki       https://arthas.aliyun.com/doc
tutorials  https://arthas.aliyun.com/doc/arthas-tutorials.html
version    3.5.4
main_class
pid        38220
time       2021-10-17 23:30:31
​
[arthas@38220]$

4.运行trace命令

trace -E com.example.study.controller.ResourceController test|uploadFileToLocal --skipJDKMethod false

其中:

-E:正则匹配函数

com.example.study.controller.ResourceController:被诊断的类

test|uploadFileToLocal:被诊断的方法(test和uploadFileToLocal)

--skipJDKMethod:是否需要跳过JDK方法(默认为true)

5.访问对应的接口,观察Arthas输出,如:

Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 2) cost in 93 ms, listenerId: 8
`---ts=2021-10-18 00:55:32;thread_name=http-nio-8080-exec-3;id=14;is_daemon=true;priority=5;TCCL=org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedWebappClassLoader@318ba8c8
    `---[1.7917ms] com.example.study.controller.ResourceController:test()
        +---[0.1067ms] java.io.File:<init>() #29
        +---[0.2952ms] java.io.FileReader:<init>() #30
        +---[0.124699ms] java.io.BufferedReader:<init>() #31
        +---[0.009499ms] java.lang.StringBuilder:<init>() #32
        +---[min=0.0021ms,max=0.132899ms,total=0.352502ms,count=45] java.io.BufferedReader:readLine() #34
        +---[min=0.001999ms,max=0.042ms,total=0.201797ms,count=44] java.lang.StringBuilder:append() #35
        `---[0.0196ms] java.lang.StringBuilder:toString() #37

参考文档:

Arthas 用户文档

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值