五、**hprof(Heap/CPU Profiling Tool)
hprof能夠展現(xiàn)CPU使用率,統(tǒng)計堆內(nèi)存使用情況。
語法格式如下:
java -agentlib:hprof[=options] ToBeProfiledClass
java -Xrunprof[:options] ToBeProfiledClass
javac -J-agentlib:hprof[=options] ToBeProfiledClass
完整的命令選項如下:
Option Name and Value Description Default
heap=dump|sites|all heap profiling all
cpu=samples|times|old CPU usage off
monitor=y|n monitor contention n
format=a|b text(txt) or binary output a
file= write data to file java.hprof[.txt]
net=: send data over a socket off
depth= stack trace depth 4
interval= sample interval in ms 10
cutoff= 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 y
verbose=y|n print messages about dumps y
來幾個官方指南上的實例。
CPU Usage Sampling Profiling(cpu=samples)的例子:
java -agentlib:hprof=cpu=samples,interval=20,depth=3 Hello
上面每隔20毫秒采樣CPU消耗信息,堆棧深度為3,生成的profile文件名稱是java.hprof.txt,在當前目錄。
CPU Usage Times Profiling(cpu=times)的例子,它相對于CPU Usage Sampling Profile能夠獲得更加細粒度的CPU消耗信息,能夠細到每個方法調(diào)用的開始和結(jié)束,它的實現(xiàn)使用了字節(jié)碼注入技術(shù)(BCI):
javac -J-agentlib:hprof=cpu=times Hello.java
Heap Allocation Profiling(heap=sites)的例子:
javac -J-agentlib:hprof=heap=sites Hello.java
Heap Dump(heap=dump)的例子,它比上面的Heap Allocation Profiling能生成更詳細的Heap Dump信息:
javac -J-agentlib:hprof=heap=dump Hello.java
雖然在JVM啟動參數(shù)中加入-Xrunprof:heap=sites參數(shù)可以生成CPU/Heap Profile文件,但對JVM性能影響非常大,不建議在線上服務(wù)器環(huán)境使用