Java 内存诊断比较容易, 需要:
1 获取heap dump
2 分析heap dump
1.1 获取dump之1 VM arguments: -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./java_pid<pid>.hprof
reference for VM options: http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html
如果不指定, dump文件默认输出到user.dir中, 可用jinfo <pid>获取user.dir变量设置。
java.lang.OutOfMemoryError: Java heap space
Dumping heap to java_pid8096.hprof ...
Heap dump file created [168062394 bytes in 1.306 secs]
注意得到文件后,可用下面命令进行分析:
jmap -histo ./java_pid<pid>.hprof
1.2 获取dump之2 jmap/jcmd
jmap -dump:format=b,file=snapshot.jmap <pid>
jcmd <process id/main class> GC.heap_dump filename=Myheapdump
或者直接获取(histogram):
jcmd <pid> GC.class_histogram
jmap -histo <pid>
1.3 获取dump之3 Jconsole + HotSpotDiagnosticMXBean (安装JDK)
注意文件存储在 user.dir下面。(用jinfo可以获取)
2.1 分析dump之Jhat(需要JDK)
jhat -J-Xmx1024m snapshot.jmap //-J-Xmx1024 是针对内存dump文件太大而无法启动Jhat的情况
BaseType Character
Type | Interpretation | |
B | byte | signed byte |
C | char | Unicode character |
D | double | double-precision floating-point value |
F | float | single-precision floating-point value |
I | int | integer |
J | long | long integer |
L<classname>; | reference | an instance of class de><classname>de> |
S | short | signed short |
Z | boolean | de>truede> or de>falsede> |
[ | reference | one array dimension |
其他分析dump的方法,如JFR(Java Flight Recorder), 请参见另一篇专题。
referenc:
http://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/memleaks.html#CIHGFAEG
http://www.cnblogs.com/duanxz/p/4901437.html
来源:https://www.cnblogs.com/bjfarmer/p/5505298.html