Java 内存诊断比较容易, 需要:
1 获取heap dump
2 分析heap dump
获取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中。
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
获取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>
获取dump之3 Jconsole + HotSpotDiagnosticMXBean (安装JDK)
注意文件存储在 user.dir下面。(用jinfo可以获取)
分析dump之Jhat(需要JDK)
referenc:
http://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/memleaks.html#CIHGFAEG