Arthas heapdump命令

Arthas

Arthas 是Alibaba开源的Java诊断工具,深受开发者喜爱。在线排查问题,无需重启;动态跟踪Java代码;实时监控JVM状态。

Arthas 支持JDK 6+,支持Linux/Mac/Windows,采用命令行交互模式,同时提供丰富的 Tab 自动补全功能,进一步方便进行问题的定位和诊断。

当你遇到以下类似问题而束手无策时,Arthas可以帮助你解决:

  • 这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception?
  • 我改的代码为什么没有执行到?难道是我没 commit?分支搞错了?
  • 遇到问题无法在线上 debug,难道只能通过加日志再重新发布吗?
  • 线上遇到某个用户的数据处理有问题,但线上同样无法 debug,线下无法重现!
  • 是否有一个全局视角来查看系统的运行状况?
  • 有什么办法可以监控到JVM的实时运行状态?
  • 怎么快速定位应用的热点,生成火焰图?

本教程会以一个简单的应用为例,演示heapdump命令。

启动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-bootArthas的启动程序,它启动后,会列出所有的Java进程,用户可以选择需要诊断的目标进程。

选择第一个进程,输入 1 ,再Enter/回车

1
1

Attach成功之后,会打印Arthas LOGO。输入 help 可以获取到更多的帮助信息。

1
help

Arthas Boot

heapdump命令

dump java heap, 类似jmap命令的heap dump功能。

使用参考

dump到指定文件
1
heapdump /tmp/dump.hprof
1
2
3
[arthas@58205]$ heapdump /tmp/dump.hprof
Dumping heap to /tmp/dump.hprof...
Heap dump file created
只dump live对象
1
heapdump --live /tmp/dump.hprof
1
2
3
[arthas@58205]$ heapdump --live /tmp/dump.hprof
Dumping heap to /tmp/dump.hprof...
Heap dump file created

dump到临时文件

1
heapdump
1
2
3
[arthas@58205]$ heapdump
Dumping heap to /var/folders/my/wy7c9w9j5732xbkcyt1mb4g40000gp/T/heapdump2019-09-03-16-385121018449645518991.hprof...
Heap dump file created

更多信息

在“heapdump”中,我们演示了了Arthas的heapdump命令。如果有更多的技巧或者使用疑问,欢迎在Issue里提出。

如果您在使用Arthas,请让我们知道。您的使用对我们非常重要:查看

文章拷贝来源:https://start.aliyun.com/course?spm=a2ck6.17690074.0.0.28bc2e7dHTphXs&id=PaiFAkJM