How to do thread dump on Out Of Memory Error

Petro Semeniuk picture Petro Semeniuk · Mar 4, 2013 · Viewed 10.4k times · Source

I know that -XX:+HeapDumpOnOutOfMemoryError will enable heap dump on OutOfMemoryError.

Is there anything similar for thread dump? I need this in case when error happens on server - I don't have access to process itself when it happens

Answer

zagyi picture zagyi · Mar 4, 2013

The exact point of execution where an OutOfMemoryError is raised is very much random (unless you have a single threaded application), so I don't think it would make too much sense to get a thread dump upon OOM. At least this is true if it's the 'Java heap space' sort of OOM, but probably for the other sorts of OOM errors as well.

Anyway, take a look at this older post addressing the same question.

However a better way to tackle an OOM error is to use the Eclipse Memory Analyzer Tool to inspect the heapdump. It helps you to identify memory leaks by transforming the object graph into a so-called dominator tree. This structure reveals the keep-alive dependencies among objects, so it becomes very easy to identify the ones responsible for retaining the biggest chunks of memory (see Shallow vs. Retained Heap).