-agentlib:hprof是堆內存和C的抽樣代理工具,在控制檯執行java -agentlib:hprof=help能夠看到詳細的使用說明,以下:java
選項的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 ysocket
廢棄選項
----------------
gc_okay=y|n工具
舉例:url
a) 每20ms獲取CUP抽樣信息,跟蹤3層堆棧spa
java -agentlib:hprof=cpu=samples,interval=20,depth=3 classname.net
b) 基於allocation sites方式的內存分配
java -agentlib:hprof=heap=sites classname命令行
c) 命令行啓動參數代理
備註:orm
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