Thursday 16 August 2018

jmap: -dump:: Dump java heap in hprof binary format

jmap command provides ‘-dump:<dump-options>’ to dump java heap in hprof binary format.

Below table summarizes different <dump-options> available with -dump command.

Option
Description
live
Dump only live objects
format=b
Generate the dump in binary format
file=<file>
Dump heap to this file

For example,

HelloWorld.java
public class HelloWorld {
 public static void main(String args[]) throws Exception {
  int count = 0;
  
  Thread t1 = new Thread() {
   public void run() {
    while(true){
     try{
      Thread.sleep(2000);
     }catch(Exception e){
      
     }
    }
   }
  };

  t1.start();
 }
}


Compile and run HelloWorld application.

Open another command prompt and execute jcmd command.
C:\>jcmd
18612
21764 sun.tools.jcmd.JCmd
2600 HelloWorld


As you see the output of jcmd command, HelloWorld application is running with process id 2600.

Use below syntax to generate the dump.
jmap -dump:live,format=b,file=heap.bin <pid>
C:\Users\krishna\Documents\Study\Miscellaneous>jmap -dump:live,format=b,file=heap.bin  2600
Dumping heap to C:\Users\krishna\Documents\Study\Miscellaneous\heap.bin ...
Heap dump file created





Previous                                                 Next                                                 Home

No comments:

Post a Comment