jmap使用方法及原理
jmap使用方法及原理
在这篇文章中:
1. jmap常用命令
在cmd或者shell窗口输入jmap -h,输出如下结果:
Usage: jmap [option] <pid> (to connect to running process) jmap [option] <executable <core> (to connect to a core file) jmap [option] [[email protected]]<remote server IP or hostname> (to connect to remote debug server) where <option> is one of: <none> to print same info as Solaris pmap -heap to print java heap summary -histo[:live] to print histogram of java object heap; if the "live" suboption is specified, only count live objects -clstats to print class loader statistics -finalizerinfo to print information on objects awaiting finalization -dump:<dump-options> to dump java heap in hprof binary format dump-options: live dump only live objects; if not specified, all objects in the heap are dumped. format=b binary format file=<file> dump heap to <file> Example: jmap -dump:live,format=b,file=heap.bin <pid> -F force. Use with -dump:<dump-options> <pid> or -histo to force a heap dump or histogram when <pid> does not respond. The "live" suboption is not supported in this mode. -h | -help to print this help message -J<flag> to pass <flag> directly to the runtime system
1. jmap -heap
查看java堆的信息以及垃圾回收算法等
可以看到堆的分代情况以及各区域的使用情况,垃圾收集器使用的是Parallel GC与java -XX:+PrintCommandLineFlags -version得到的结果一致:
2. jmap -histo[:live]
打印当前java堆中所有对象的实例数和大小等,加了live选项则只打印存活的对象
3. jmap -clstas
打印class loader的统计信息
4. jmap -finalizerinfo
打印在等待执行finalize方法的对象
5. jmap -dump:<dump-options>
dump-options主要有:
- live 只dump存活的对象,如果不加则会dump所有对象
- format=b 表示以二进制格式
- file=filepath 输出到某个文件中
把java堆中的对象dump到本地文件,然后使用第三方工具进行分析,如MAT,JProfile,IBM的分析工具等
6. jmap -F
与-dump 和-histo一起使用,强制执行后者 jmap -F -dump:live,format=b,file=heap.bin
2. 与jvm通信方式attach机制
attach 就是jvm提供一种jvm进程间通信的能力,能让一个进程传命令给另外一个进程,并让它执行内部的一些操作:
- VirtualMachine.attach: 位于tools.jar,jstack和jhipcup的attach使用的是VirtualMachine.attach,详情参考笨神的JVM源码分析之Attach机制实现完全解读:https://mp.weixin.qq.com/s?__biz=MzIzNjI1ODc2OA==&mid=2650886799&idx=1&sn=108c5fdfcd2695594d4f80ff02fc9a70&mpshare=1&scene=21&srcid=0114WsKpUmDXhRtqy8x7JX5w#wechat_redirect
- Serviceability Agent,简称SA 继承Tool/HotSpotAgent.attach。SA 在JDK中是以Jar文件的形式提供的,位于JAVA_HOME/lib/sa-jdi.jar ,和一般的Jar文件执行一样。SA工具一般使用ptrace方法来进行attach和detach。详情参考:https://www.jianshu.com/p/542e50edc8e3
- Perf.getPerf().attach:位于rt.jar,通过PerfData文件获取信息。 参考:https://www.jianshu.com/p/542e50edc8e3
3. jmap操作所使用的attach机制
- -dump和-histo[:live] 默认使用的是VirtualMachine.attach
- -pmap 、-heap、-heap:format=b、-clstats、-finalizerinfo、-J默认使用的是SA
- -F可以把默认使用VirtualMachine.attach的方式改成SA attach方式