Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request java heap dump (core dump) from within application

I need a way to request a heap dump from within the application.

Rationale: When I encounter a specific error condition, I'd like to dump heap, so that I can see what is holding on to the memory.

But I would like to automate this (For example, when I detect that some specific condition has occurred. Or when a watchdog doesn't gets its pings anymore. When some test fails). Thus I need a way to dump the heap from within the application itself. I can't seem to find it with the MX beans stuff. Although the MX Beans can give very nice stack traces with monitor and "ownable synchronizer" info, deadlock and contention info, I can't seem to find a way to request a heap dump. Are there any such way? Or by some indirect means, for example how does these JVisualVM things do it? And one can tell the JVM to dump core on OutOfMemoryExceptions..?

like image 718
stolsvik Avatar asked Nov 30 '09 10:11

stolsvik


People also ask

How do I pull a heap dump?

Heap dump = memory contents for the JVM process output to a binary file. To take a thread dump on Windows, CTRL + BREAK if your JVM is the foreground process is the simplest way. If you have a unix-like shell on Windows like Cygwin or MobaXterm, you can use kill -3 {pid} like you can in Unix.

Where can I get a heap dump?

The JDK comes with several tools to capture heap dumps in different ways. All these tools are located under the bin folder inside the JDK home directory.


2 Answers

If it's not enough to dump heap on OutOfMemoryError, there's a HotSpot-dependent way of programmatically dumping heap from Java applications, if that's OK.

like image 107
gustafc Avatar answered Sep 20 '22 06:09

gustafc


What about using the VM option -XX:+HeapDumpOnOutOfMemoryError to tell the HotSpot VM to generate a heap dump when it runs out of memory?

like image 38
Pascal Thivent Avatar answered Sep 23 '22 06:09

Pascal Thivent