Alternatives for heapdumps creation with higher performance than jmap?

I have to create heapdumps, which works nice with jmap. My problem is, that jmap takes very long to create the heapdump file. Especially when the heap is getting bigger (> 1GB) it is taking too long.

One situation as example:
When the server gets into trouble with the heapspace, I want to restart it automatically and create a heapdump before the restart. This works, but takes too long to write the heapdump. This way the server is down for too long. The heapdump creation takes longer than one hour.

I know about -XX:+HeapDumpOnOutOfMemoryError, but most of the time I can find the memory problem before the exception is thrown by the jvm.

Is there an alternative to jmap which writes the heapdumps faster?
A special solution for the example above would also be appreciated.

This question is a mix between programming and system-administration, but I think I'm at the right place here.


Solution 1:

i found an answer to my question. this answer to an other question on serverfault gave me the idea.

  1. connect with gdb to your java process
    gdb --pid=<your java pid>
  2. create core dump from gdb
    gcore <file name>
    detach
    quit
  3. restart the java process or do what ever you like
  4. create a heap dump from the core dump, by connecting jmap to the core dump
    jmap -heap:format=b <path to java binary> <core dump file>

In step 4 it's vital you specify the right java binary, otherwise jmap can not attach to the core dump. if you are not sure which binary was used for the java process, open the core dump with gdb:
gdb --core=<core dump file>
There will be a line like this one, telling you the complete path:
Core was generated by '/opt/tomcat/bin/jsvc'.

creating the core dump is much faster, than creating the heapdump directly via jmap. this way you can create a heapdump of a java process without too long downtimes.

EDIT:
when you get the following error message, it might be you have specified the wrong java binary:

Error attaching to core file: Can't attach to the core file

to get the right java binary for the jmap call, open the core dump with gdb:

gdb --core=[path tp core file]

there will be a line like this, telling you the correct binary:

Core was generated by `/opt/tomcat/bin/jsvc'.