-agentlib:hprof是堆内存和C的抽样代理工具,在控制台执行java -agentlib:hprof=help可以看到详细的使用说明,如下:
选项的key和value 描述 默认值
--------------------- ----------- -------
heap=dump|sites|all 堆内存使用抽样 all
cpu=samples|times|old CPU使用情况 off
monitor=y|n monitor contention n
format=a|b text(txt) or binary output a
file=<file> write data to file java.hprof[{.txt}]
net=<host>:<port> send data over a socket off
depth=<size> 栈跟踪深度 4
interval=<ms> 抽样间隔时间ms 10
cutoff=<value> output cutoff point 0.0001
lineno=y|n line number in traces? y
thread=y|n thread in traces? n
doe=y|n dump on exit? y
msa=y|n Solaris micro state accounting n
force=y|n force output to <file> y
verbose=y|n print messages about dumps y
废弃选项
----------------
gc_okay=y|n
举例:
a) 每20ms获取CUP抽样信息,跟踪3层堆栈
java -agentlib:hprof=cpu=samples,interval=20,depth=3 classname
b) 基于allocation sites方式的内存分配
java -agentlib:hprof=heap=sites classname
c) 命令行启动参数
备注:
format=b不能和monitor=y一起使用
format=b不能和cpu=old|times一起使用
java -Xrunhprof:[help]|[<option>=<value>, ...]使用方式和 java -agentlib:hprof=[help]|[<option>=<value>, ...]操作结果是一样的
申明:
这种方式只是JVMTI接口的示范性代码,是BCI的使用方式,不是官方的product或者标准JDK部分
-Xrunhprof接口会在未来的release版本中移除
format=b是试验性的处理格式,可能会在未来的release版本中修改
实践:
通过java -agentlib:hprof=heap=sites classname方式执行时,classname是编译后的class类名,才能得到预期的执行结果。
CPU TIME (ms) BEGIN (total = 11094) Wed Nov 9 15:17:55 2016
rank self accum count trace method
1 90.14% 90.14% 1 301243 HProfTest.slowerMethod
2 9.01% 99.15% 1 301244 HProfTest.slowMethod
3 0.04% 99.19% 1 300163 java.lang.invoke.MethodHandle.<clinit>
4 0.03% 99.22% 9 300421 java.io.UnixFileSystem.normalize
5 0.03% 99.24% 6 300837 sun.misc.PerfCounter.<init>
6 0.02% 99.26% 26 300053 java.lang.String.hashCode
7 0.02% 99.28% 10 300316 sun.net.util.URLUtil.urlNoFragString
参考资料:
原文链接:http://www.jianshu.com/p/6df2963fe94e