sun jdk 下如何生成heapdump文件
以Tomcat为例:
在tomcat启动脚本中增加 HeapDumpOnOutOfMemoryError 参数
此参数需要Java SE release 5.0 update 7 或以上支持
设置示例:
set JAVA_OPTS=%JAVA_OPTS% -server -Xms512m -Xmx800m -XX:PermSize=64M -XX:MaxPermSize=128m -Djava.awt.headless=true -XX:+HeapDumpOnOutOfMemoryError
测试内存溢出时可以把-Xms和-Xmx等的值相应调低即可.
文件默认产生在tomcat/bin目录下
如增加HeapDumpOnCtrlBreak Option就可以通过ctrl+break或收到SIGQUIT生成heapdump文件
此参数需要Java SE release 5.0 update 14 或以上支持
设置示例:
set JAVA_OPTS=%JAVA_OPTS% -server -Xms512m -Xmx800m -XX:PermSize=64M -XX:MaxPermSize=128m -Djava.awt.headless=true -XX:+HeapDumpOnOutOfMemoryError -XX:+HeapDumpOnCtrlBreak
参数介绍:
HeapDumpOnOutOfMemoryError 参数:
The -XX:+HeapDumpOnOutOfMemoryError command-line option was introduced in Java SE release
5.0 update 7. This option tells the HotSpot VM to generate a heap dump when the first thread throws a
java.lang.OutOfMemoryError because the Java heap or the permanent generation is full. There is
no overhead in running with this option, and so it can be useful for production systems where
OutOfMemoryError takes a long time to surface.
The heap dump is in HPROF binary format, and so it can be anaylzed by any tool that can import this
format, for example the Heap Analysis Tool (HAT).
By default the heap dump is created in a file called java_pid<pid>.hprof in the working directory
of the VM, where <pid> is the process ID. You can specify an alternative file name or directory with
the -XX:HeapDumpPath= option. For example, -XX:HeapDumpPath=/disk2/dumps will cause the
heap dump to be generated in the /disk2/dumps directory.
HeapDumpOnCtrlBreak 参数:
The -XX:+HeapDumpOnCtrlBreak command-line option was introduced in Java SE release 5.0 update 14. This option tells the HotSpot VM to generate a heap dump when a Ctrl-Break or SIGQUIT signal
is received. This provides a way to trigger a heap dump on demand.
The heap dump is in HPROF binary format, and so it can be anaylzed by any tool that can import this
format, for example the Heap Analysis Tool (HAT). The heap dump contains only live objects.
By default the heap dump is created in a file called java_pid<pid>.hprof.<yyyymmdd>.<hhmmss>
in the working directory of the VM, where <pid> is the process ID, and <yyyymmdd>.<hhmmss> is the
approximate time when the heap dump was generated.
You can specify an alternative file name or directory with the -XX:HeapDumpPath= option. For
example, -XX:HeapDumpPath=/disk2/dumps will cause the heap dump to be generated in the
/disk2/dumps directory.
If both the -XX:+HeapDumpOnCtrlBreak and -XX:+PrintClassHistogram options are enabled, the
heap dump and the heap histogram are obtained from the same heap snapshot.
详见JavaTM 2 Platform, Standard Edition 5.0 Troubleshooting and Diagnostic Guide
http://java.sun.com/j2se/1.5/pdf/jdk50_ts_guide.pdf