How to reliably take Java Heap Dumps?

Solution 1:

Which is your operating system? (I can't add comments).

For Solaris we get better results first forcing a core dump (gcore <pid>) and then attaching jmap to the core dump file (jmap -heap:format=b <path to java bin> <path to core>)

gcore is a *nix utility to generate an image of a running program. See link.

Solution 2:

we have a JSP that queries ManagementFactory.getThreadMXBean() and produces a report. May not be useful when the app has crashed, but if you poll every minute or so, you'll get an idea of what's happening.

More info here.