The HotSpot JVM has a very useful command line flag: –XX:+HeapDumpOnOutOfMemoryError
. As the name suggests, this flag makes Java automatically dump its heap memory on an OutOfMemoryError
This flag is great for systems that bomb out when you are sleeping. In the morning you'll not only find the exception in the logs, but also an nice, fat heap dump to throw your analysis tools at.
This flag may not be so great to use when you run your JVM with a very large heap. Dumping a help that is more than a few GB may take quite some time.