How to take heap dump?

11.1k views Asked by At

I want to collect heap dump on JVM crash

So i wrote a simple code

public class Test {
private String name;

public Test(String name) {
    this.name = name;
}

public void execute() {

    Map<String,String> randomData = new HashMap<String,String>();
    for(int i=0;i<1000000000;i++) {
       randomData.put("Key:" + i,"Value:" + i);
    }
}

public void addData() {
}

public static void main(String args[]) {
    String myName = "Aniket";
    Test tStart = new Test(myName);
    tStart.execute();
}
}

and I am running it as follows

[aniket@localhost Desktop]$ java -cp . -Xms2m -Xmx2m Test
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at Test.execute(Test.java:15)
    at Test.main(Test.java:25)

I got OutOfMemoryError which I wanted but there is no heap dump in the working directory(like hs_err_pidXXXX.log which I expected). What am I missing? How do I get a heap dump?

Update :

I tried -XX:ErrorFile=. still no use. If above is not the way to get the heap dump(Crash JVM) how can I crash my JVM to get those logs?

6

There are 6 answers

3
Peter Lawrey On BEST ANSWER

You are confusing an exception or error being thrown as a JVM crash.

A JVM crash occurs due to an internal error in the JVM, you cannot trigger this by writing a normal Java program (or should not unless you find a bug)

What you are doing is triggering an Error which means the program continues to run until all the non daemon threads exit.

The simplest tool to examine the heap is VisualVM which comes with the JDK. If you want to trigger a heap dump on an OutOfMemoryError you can use -XX:+HeapDumpOnOutOfMemoryError

0
Narendra Pathai On

Use Jmap

jmap [options] pid

pid is the process id of application

0
Levente Kurusa On

Eclipse has an awesome Heap Analyzer

Also, you can use jps to get the PID, and then jmap for the heap itself.

In case, you want to crash the JVM, your best guess would be native code.

0
sr01853 On

When you see the below

Exception in thread "main" java.lang.OutOfMemoryError

It means your error or exception is handled by the exception handler. This is not a crash.

0
Jim T On

You can pass below JVM arguments to your application:

-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=

This argument will automatically trigger heap dump in the specified 'file-path' when your application experiences OutOfMemoryError. There are 7 different options to take heap dumps from your application:

  1. jmap
  2. -XX:+HeapDumpOnOutOfMemoryError
  3. jcmd
  4. JVisualVM
  5. JMX
  6. Programmatic Approach
  7. Administrative consoles

Details about each option can be found in this article. Once you have captured heap dump, you may use tools like Eclipse Memory Analysis tool, HeapHero to analyze the captured heap dumps.

0
Seeker On

Find the process id for which you want to take the heap dump

ps -ef | grep java

Once you get PID by running above command run below command to generate heap dump.

jmap -dump:format=b,file=<fileName> <java PID>