Arthas
是Alibaba开源的Java诊断工具,深受开发者喜爱。在线排查问题,无需重启;动态跟踪Java代码;实时监控JVM状态。
Arthas
支持JDK 6+,支持Linux/Mac/Windows,采用命令行交互模式,同时提供丰富的 Tab
自动补全功能,进一步方便进行问题的定位和诊断。
当你遇到以下类似问题而束手无策时,Arthas可以帮助你解决:
这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception?
我改的代码为什么没有执行到?难道是我没 commit?分支搞错了?
遇到问题无法在线上 debug,难道只能通过加日志再重新发布吗?
线上遇到某个用户的数据处理有问题,但线上同样无法 debug,线下无法重现!
是否有一个全局视角来查看系统的运行状况?
有什么办法可以监控到JVM的实时运行状态?
怎么快速定位应用的热点,生成火焰图?
本教程会以一个简单的应用为例,演示jvm命令。
Github: https://github.com/alibaba/arthas
文档: https://arthas.aliyun.com/doc/
下载arthas-demo.jar
,再用java -jar
命令启动:
bash wget https://arthas.aliyun.com/arthas-demo.jar;java -jar arthas-demo.jar
arthas-demo
是一个很简单的程序,它随机生成整数,再执行因式分解,把结果打印出来。如果生成的随机数是负数,则会打印提示信息。
在新的Terminal 2
里,下载arthas-boot.jar
,再用java -jar
命令启动:
bash wget https://arthas.aliyun.com/arthas-boot.jar;java -jar arthas-boot.jar
arthas-boot
是Arthas
的启动程序,它启动后,会列出所有的Java进程,用户可以选择需要诊断的目标进程。
选择第一个进程,输入 1
,再Enter/回车
:
bash 1
Attach成功之后,会打印Arthas LOGO。输入 help
可以获取到更多的帮助信息。
bash help
jvm
命令可以查看当前JVM信息。
bash jvm
bash jvm | grep PATH console [arthas@41064]$ jvm | grep PATH CLASS-PATH packaging/target/arthas-bin/arthas-demo.jar BOOT-CLASS-PATH /Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/lib/resources.jar:/Librar LIBRARY-PATH /Users/gongdewei/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extens
console $ jvm RUNTIME -------------------------------------------------------------------------------------------------------------- MACHINE-NAME 37@ff267334bb65 JVM-START-TIME 2020-07-23 07:50:36 MANAGEMENT-SPEC-VERSION 1.2 SPEC-NAME Java Virtual Machine Specification SPEC-VENDOR Oracle Corporation SPEC-VERSION 1.8 VM-NAME Java HotSpot(TM) 64-Bit Server VM VM-VENDOR Oracle Corporation VM-VERSION 25.201-b09 INPUT-ARGUMENTS [] CLASS-PATH demo-arthas-spring-boot.jar BOOT-CLASS-PATH /usr/lib/jvm/java-8-oracle/jre/lib/resources.jar:/usr/lib/jvm/java-8-oracle/j re/lib/rt.jar:/usr/lib/jvm/java-8-oracle/jre/lib/sunrsasign.jar:/usr/lib/jvm/ java-8-oracle/jre/lib/jsse.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jce.jar:/us r/lib/jvm/java-8-oracle/jre/lib/charsets.jar:/usr/lib/jvm/java-8-oracle/jre/l ib/jfr.jar:/usr/lib/jvm/java-8-oracle/jre/classes LIBRARY-PATH /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib -------------------------------------------------------------------------------------------------------------- CLASS-LOADING -------------------------------------------------------------------------------------------------------------- LOADED-CLASS-COUNT 7529 TOTAL-LOADED-CLASS-COUNT 7529 UNLOADED-CLASS-COUNT 0 IS-VERBOSE false -------------------------------------------------------------------------------------------------------------- COMPILATION -------------------------------------------------------------------------------------------------------------- NAME HotSpot 64-Bit Tiered Compilers TOTAL-COMPILE-TIME 14921(ms) -------------------------------------------------------------------------------------------------------------- GARBAGE-COLLECTORS -------------------------------------------------------------------------------------------------------------- PS Scavenge name : PS Scavenge [count/time (ms)] collectionCount : 7 collectionTime : 68 PS MarkSweep name : PS MarkSweep [count/time (ms)] collectionCount : 1 collectionTime : 47 -------------------------------------------------------------------------------------------------------------- MEMORY-MANAGERS -------------------------------------------------------------------------------------------------------------- CodeCacheManager Code Cache Metaspace Manager Metaspace Compressed Class Space Copy Eden Space Survivor Space MarkSweepCompact Eden Space Survivor Space Tenured Gen -------------------------------------------------------------------------------------------------------------- MEMORY -------------------------------------------------------------------------------------------------------------- HEAP-MEMORY-USAGE init : 268435456(256.0 MiB) [memory in bytes] used : 18039504(17.2 MiB) committed : 181403648(173.0 MiB) max : 3817865216(3.6 GiB) NO-HEAP-MEMORY-USAGE init : 2555904(2.4 MiB) [memory in bytes] used : 33926216(32.4 MiB) committed : 35176448(33.5 MiB) max : -1(-1 B) -------------------------------------------------------------------------------------------------------------- OPERATING-SYSTEM -------------------------------------------------------------------------------------------------------------- OS Linux ARCH amd64 PROCESSORS-COUNT 3 LOAD-AVERAGE 29.53 VERSION 4.15.0-52-generic -------------------------------------------------------------------------------------------------------------- THREAD -------------------------------------------------------------------------------------------------------------- COUNT 30 DAEMON-COUNT 24 PEAK-COUNT 31 STARTED-COUNT 36 DEADLOCK-COUNT 0 -------------------------------------------------------------------------------------------------------------- FILE-DESCRIPTOR -------------------------------------------------------------------------------------------------------------- MAX-FILE-DESCRIPTOR-COUNT 1048576 OPEN-FILE-DESCRIPTOR-COUNT 100 Affect(row-cnt:0) cost in 88 ms.
COUNT
: JVM当前活跃的线程数
DAEMON-COUNT
: JVM当前活跃的守护线程数
PEAK-COUNT
: 从JVM启动开始曾经活着的最大线程数
STARTED-COUNT
: 从JVM启动开始总共启动过的线程次数
DEADLOCK-COUNT
: JVM当前死锁的线程数
MAX-FILE-DESCRIPTOR-COUNT
:JVM进程最大可以打开的文件描述符数
OPEN-FILE-DESCRIPTOR-COUNT
:JVM当前打开的文件描述符数
在“jvm”中,我们演示了了Arthas的jvm命令。如果有更多的技巧或者使用疑问,欢迎在Issue里提出。
Issues: https://github.com/alibaba/arthas/issues
文档: https://arthas.aliyun.com/doc
如果您在使用Arthas,请让我们知道。您的使用对我们非常重要:查看
文章拷贝来源:https://start.aliyun.com/course?spm=a2ck6.17690074.0.0.28bc2e7dHTphXs&id=PaiFAkJM