Arthas
是Alibaba开源的Java诊断工具,深受开发者喜爱。在线排查问题,无需重启;动态跟踪Java代码;实时监控JVM状态。
Arthas
支持JDK 6+,支持Linux/Mac/Windows,采用命令行交互模式,同时提供丰富的 Tab
自动补全功能,进一步方便进行问题的定位和诊断。
当你遇到以下类似问题而束手无策时,Arthas可以帮助你解决:
这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception?
我改的代码为什么没有执行到?难道是我没 commit?分支搞错了?
遇到问题无法在线上 debug,难道只能通过加日志再重新发布吗?
线上遇到某个用户的数据处理有问题,但线上同样无法 debug,线下无法重现!
是否有一个全局视角来查看系统的运行状况?
有什么办法可以监控到JVM的实时运行状态?
怎么快速定位应用的热点,生成火焰图?
本教程会以一个简单的应用为例,演示jvm命令。
Arthas demo 下载arthas-demo.jar
,再用java -jar
命令启动:
1 wget https://arthas.aliyun.com/arthas-demo.jar;java -jar arthas-demo.jar
arthas-demo
是一个很简单的程序,它随机生成整数,再执行因式分解,把结果打印出来。如果生成的随机数是负数,则会打印提示信息。
启动arthas-boot 在新的Terminal 2
里,下载arthas-boot.jar
,再用java -jar
命令启动:
1 wget https://arthas.aliyun.com/arthas-boot.jar;java -jar arthas-boot.jar
arthas-boot
是Arthas
的启动程序,它启动后,会列出所有的Java进程,用户可以选择需要诊断的目标进程。
选择第一个进程,输入 1
,再Enter/回车
:
Attach成功之后,会打印Arthas LOGO。输入 help
可以获取到更多的帮助信息。
jvm命令 jvm
命令可以查看当前JVM信息。
查找Java应用的classpath
1 2 3 4 [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
jvm使用参考 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 $ 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.
jvm使用参考 THREAD相关
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里提出。
如果您在使用Arthas,请让我们知道。您的使用对我们非常重要:查看
文章拷贝来源:https://start.aliyun.com/course?spm=a2ck6.17690074.0.0.28bc2e7dHTphXs&id=PaiFAkJM