2

I know one can dump a .dmp file from a C++ project. This dmp file can be openned by the Visual Studio which is VERY useful because then you can navigate the call stack looking in variables values.

Does this mechanism exist for JVM too?

user1028741
  • 2,745
  • 6
  • 34
  • 68

3 Answers3

2

You can use -XX:HeapDump JVM options.

With the following JVM options:

-XX:-HeapDumpOnOutOfMemoryError 
-XX:HeapDumpPath="/tmp"

JVM will dump the content of heap to a file in specified directory. Note that this only happens when OutOfMemoryError is thrown since dump isn't really needed if JVM crashed due to a different reason.

You may also want to read http://docs.oracle.com/javase/6/docs/technotes/tools/share/jhat.html once

Daksh Shah
  • 2,997
  • 6
  • 37
  • 71
  • @DakashShah thanks, but I would like it to happen not only when OutofMemoryError is thrown. I would like my Java server to dump it's stack&heap when it gets any kind of Exception (I.e. NullPointerException) – user1028741 Apr 08 '14 at 06:24
  • Also, is it possible, using this method, to open the dump file through the Eclipse for navigating the call stack like Visual Studio dmp files? – user1028741 Apr 08 '14 at 06:25
  • @user1028741 I have not tried this, I suppose you will need to try it on your own and check it out – Daksh Shah Apr 08 '14 at 06:26
1

How to get a JVM dump?

  1. You can use jmap whenever you want to get a JVM dump.

    jmap -dump:format=b,file=/path/to/store/dumpfile jvmpid

  2. You can add JVM option at startup to get a JVM dump when OOM.

    -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/path/to/store/dumpfile

How to analyze jvm dump file?

  1. Use Memory Analyzer (MAT)

mat

The Eclipse Memory Analyzer is a fast and feature-rich Java heap analyzer that helps you find memory leaks and reduce memory consumption.

2.Use IBM HeapAnalyzer

enter image description here

HeapAnalyzer allows the finding of a possible Java™ heap leak area through its heuristic search engine and analysis of the Java heap dump in Java applications.

lichengwu
  • 4,277
  • 6
  • 29
  • 42
  • +1 for very nice answer. I didn't marked as solved, because this is still only for tracing memory leaks, and not reveal the variables values in the stack when dumped. (I'm trying to debug other exception - not memory leaks). – user1028741 Apr 08 '14 at 21:08
0

You can remote debug (e.g. with Eclipse) your Java application which allows you to view the stack and the variable contents.

Or you could take a Thread dump (e.g. with jstack). But this does not include the variables.

Michi Gysel
  • 760
  • 6
  • 11