How to use JHAT to analyze the heat dump - Using jmap as well


Contents of page >
  • What is purpose of using jhat ?
  • 1) First Write java program - (which we will later analyze with jhat )>
  • 2) Use jps to find the vmid (virtual machine id i.e. JVM id)
  • 3) Now we will how to use jmap and jhat
    • 3.1) Use jmap - to generate heap dump  
      • From java docs about jmap
      • Full form of jmap>
      • Location of jmap?
      • Use following command to generate heap dump >
    • 3.2) Use jhat - to analyze the heap dump
      • From java docs about jhat
      • Full form of jhat>
      • Location of jhat?
      • Now we will type jhat command to analyze the heap dump >
  • 4) Opening server on specific port >
  • 5) Once we go to http://localhost:7000/
  • 6) See heap information - objects formed in our java program



What is purpose of using jhat ?
Jhat is used for analyzing the garbage collection dump in java.

In this tutorial we will learn how to use jmap - to generate heap dump. And,
Use jhat - to analyze the heap dump

1) First Write java program - (which we will later analyze with jhat )>
package garbageCollection;

import java.util.ArrayList;
import java.util.List;
public class GarbageCollectionAnalysisExample{
   public static void main(String[] args) {
          List<String> l = new ArrayList<String>();
          for (int i = 0; i < 100000000; i++) {
                 l = new ArrayList<String>(); //Memory leak
                 System.out.println(l);
          }
          System.out.println("Done");
   }
}

Execute the above java program. And this program will take long time to complete (Because of for loop).
And while program is executing follow step 2 (i.e. before program execution completes - If we execute below steps after programs execution is over - output will be command not found).


2) Use jps to find the vmid (virtual machine id i.e. JVM id)

What is jps?
JPS is Java Virtual Machine Process Status Tool.
The jps tool lists the HotSpot Java Virtual Machines (JVMs) running on system.


Location of jps?
jps is located in {javaHome\bin}

In my system jps is located in C:\Program Files\Java\jdk1.8.0_05\bin\jps.exe


Go to CMD and type below commands >

C:\>jps
18588 Jps
17252 GarbageCollectionAnalysisExample
16048
2084 Main


C:\>

17252 is the vmid which we need.


3) Now we will how to use jmap and jhat

3.1) Use jmap - to generate heap dump  

From java docs about jmap
jmap prints shared object memory maps or heap memory details of a given process or core file or a remote debug server”

Full form of jmap>
Jmap stands for Memory map.

Location of jmap?
jmap is located in {javaHome\bin}

In my system jps is located in C:\Program Files\Java\jdk1.8.0_05\bin\jmap.exe


Use following command to generate heap dump >
jmap -dump:file=E:\heapDump.jmap 17252

Where 17252 is the vmid  (picked from above).

C:\>
C:\>jmap -dump:file=E:\heapDump.jmap 17252
Dumping heap to E:\heapDump.jmap ...
Heap dump file created

C:\>


Heap dump will be generated in E:\heapDump.jmap



3.2) Use jhat - to analyze the heap dump generated using jmap in above step.


From java docs about jhat
The jhat command parses a Java heap dump file and starts a web server. The jhat command lets you to browse heap dumps with your favorite web browser. The jhat command supports pre designed queries such as show all instances of a known class MyClass, and Object Query Language (OQL). OQL is similar to SQL, except for querying heap dumps. Help on OQL is available from the OQL help page shown by the jhat command. With the default port, OQL help is available at http://localhost:7000/oqlhelp/

Full form of jhat>
Jhat stands for Java Heap Analysis Tool.


Location of jhat?
jhat is located in {javaHome\bin}

In my system jps is located in C:\Program Files\Java\jdk1.8.0_05\bin\jhat.exe


Now we will type jhat command to analyze the heap dump >
jhat E:\heapDump.jmap

C:\>jhat E:\heapDump.jmap
Reading from E:\heapDump.jmap...
Dump file created Mon Nov 07 23:59:19 IST 2016
Snapshot read, resolving...
Resolving 241865 objects...
Chasing references, expect 48 dots................................................
Eliminating duplicate references................................................
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.


By default, it will start http server on port 7000.
Then we will go to http://localhost:7000/




4) Opening server on specific port >
C:\>jhat -port 7001 E:\dump.jmap

Then it will start http server on port 7001. Then go to http://localhost:7001/



5) Once we go to http://localhost:7000/

We will see screen like this

Here you will find information like -
Note : all these links are pointing to localhost, so they won’t work on your system until you start your server following the step 3.2.

6) See heap information - objects formed in our java program

Where we can find all the objects which were created in java program.
And the diagram clearly clearly shows that there were too many instance/objects of java.util.ArrayList and java.util.ArrayList$Itr class.
So, they are clearly creating the memory leak (As per program we have used in section 1 of this tutorial). So, you can according fix your code.


Summary -

So in this core java tutorial we learned how to use JHAT to analyze and monitor the heat dump - Using jmap as well in java.


Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



RELATED LINKS>

Most important and frequently used VM (JVM) PARAMETERS with examples in JVM Heap memory in java

No comments:

Post a Comment