jps(Java Virtual Machine Process Status Tool)
-q 不輸出類名、Jar名和傳入main方法的參數
-m 輸出傳入main方法的參數(輸出全部參數)
-l 輸出main類或Jar的全限名
-v 輸出傳入JVM的參數
jstack
jstack主要用來查看某個Java進程內的線程堆棧信息。
jstack [option] pid
jstack [option] executable core
jstack [option] [server-id@]remote-hostname-or-ip
-l long listings,會打印出額外的鎖信息,在發生死鎖時可以用jstack -l pid來觀察鎖持有情況
-m mixed mode,不僅會輸出Java堆棧信息,還會輸出C/C++堆棧信息(比如Native方法)
jmap(Memory Map)和jhat(Java Heap Analysis Tool)
# 如果運行在64位JVM上,可能需要指定-J-d64命令選項參數
# 打印進程的類加載器和類加載器加載的持久代對象信息,輸出:類加載器名稱、對象是否存活(不可靠)、對象地址、父類加載器、已加載的類大小等信息
jmap -permstat pid
# 查看進程堆內存使用情況,包括使用的GC算法、堆配置參數和各代中堆內存使用情況
jmap -heap pid
# 查看堆內存中的對象數目、大小統計直方圖,如果帶上live則只統計活對象
jmap -histo[:live] pid
# 將jmap信息保存至文件中
jmap -dump:format=b,file=dumpFileName pid
jmap -dump:format=b,file=./spark.jmap 45161
# 通過jhat進行查看
jhat -port 9988 -J-Xmx2048m ./spark.jmap
jstat(JVM統計監測工具)
jstat [ generalOption | outputOptions vmid [interval[s|ms] [count]] ]
# 輸出結果代表字段
S0C、S1C、S0U、S1U:Survivor 0/1區容量(Capacity)和使用量(Used)
EC、EU:Eden區容量和使用量
OC、OU:年老代容量和使用量
PC、PU:永久代容量和使用量
YGC、YGT:年輕代GC次數和GC耗時
FGC、FGCT:Full GC次數和Full GC耗時
GCT:GC總耗時
hprof(Heap/CPU Profiling Tool)
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=<file> write data to file java.hprof[.txt]
net=<host>:<port> send data over a socket off
depth=<size> stack trace depth 4
interval=<ms> sample interval in 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
Profile
If you only like to generate JVM flame graphs, the following tools are needed:
JDK9 or JDK8 update 60 build 19
Honest Profiler, https://github.com/jvm-profiling-tools/honest-profiler/wiki
Flame graph tool, https://github.com/brendangregg/FlameGraph
參考:
- Java in Flames, Netflix Blog, https://medium.com/netflix-techblog/java-in-flames-e763b3d32166
- CPU Flame Graphs, Brendan Gregg Blog,http://www.brendangregg.com/FlameGraphs/cpuflamegraphs.html
- Linux Perf examples, Brendan Gregg Blog, http://www.brendangregg.com/perf.html
- Honest Profiler, Richard Warburton,https://github.com/jvm-profiling-tools/honest-profiler/wiki
- perf-map-agent, https://github.com/jvm-profiling-tools/perf-map-agent
- Presentation about Java Flamegraphs, Nitsan Wakart,https://2017.javazone.no/program/56179b136b91458a843383e13fd2efa1