Heap dump generation & Analysis using JMAP, JMAT, JvisualVM Tools
Jayvardhan Reddy Vanchireddy
Senior Data Engineer at Cognizant?? | Ex-Honeywell | #ONO ?? | #Azure ? | #German B1 Level Certified ???? | Writer@Medium ? | #BigData Engineer ??
Every Programmer is bound to use these tools at some point of time, As it plays a vital role in optimizing the application performance, maintain resources efficiently and lookup security threats that are involved in a real-time application.
What is a Heap Dump?
A heap dump is a snapshot of the memory of a Java Process at a single point in time. This contains data about Java objects, classes in the heap, class, fields, references, class loader names, static content, Thread Stacks, etc.
What are the benefits of generating a Heap Dump?
There are various reasons but these two stand out among others. While performing performance analysis on an application, performing a heap dump during certain execution phases will provide you with critical information on the state of the Java Process, such as object allocation on the heap and thread states. Second, when an application crashes due to a Java java.lang.OutOfMemoryError, you can enforce the JVM to perform a snapshot and capture the application’s state via a heap dump. This heap dump will typically be placed into a java_pid*[id].hprof file.
You can then load the heap dump file into a heap-dump analyzer tool to understand the java applications state — This provides a good insight and clue as to why the application crashed during production or customer use and ease further analysis.
Steps to generate HeapDump
- We require the Process Id of application, It can be obtained as shown below
a) For Windows: You can open the Task Manager and check for the process id (PID) of Jvm or use the jps command from java/bin path in command prompt.
b) For Linux: You can execute the below command to obtain the PID
ps –ef | grep jvm
2. We have to use the JMAP tool to generate the heap dump which is part of the jdk.
a) Windows: It is present inside the jdk - bin folder.
b) Linux: In production servers or real-time working environment it has JRE to run the application server and not JDK as part of it, so you will have to install JDK explicitly in it. The jmap is available inside java/bin folder as shown below.
eg: /home/perfMon/java/bin
Now that you have obtained the process id (PID) of JVM and know the location of jmap inside JDK, execute the below jmap command to generate the heap dump. It can be generated in two extensions either as .bin or as .hprof. To execute the command you need to be the Superuser or the User that has initiated the JVM.
3) Now that the heap dump is generated, we have to further analyse this heap dump. There are various tools are available to do it, but we are going to make use of 2 tools as part of this example.
a) Java VisualVM:
Java VisualVM is a tool that provides a visual interface for viewing detailed information about Java applications while they are running on a Java Virtual Machine (JVM), and for troubleshooting and profiling these applications. This includes objects allocated on the Heap, Thread state, Execution environment information, Stack etc. We can use the jvisualVM GUI tool to connect to local or remote JAVA processes.
You can download this tool using the link below.
- To integrate with eclipse as a Plugin
- You can use below link to run it as a separate application.
The way to integrate the tool with eclipse is as below
Unzip the downloaded visualvm file.
i) Open eclipse
ii) Navigate to help
iii) Install New Software
iv) Click on add and point to the file location as shown below
click next and it will install or add the VisualVM feature to eclipse
Now navigate to preferences
i) Run/Debug
ii) Launching
iii) VisualVM Config and point the locations to the visualVM executable and the JDK home directory as shown below.
After writing a sample program that you want to perform a heap-dump analysis on click on debug Configuration Select the preferred launcher from below option as VisualVM.
Once you click on OK and apply. The debugger will automatically launch the VisualVM with the application process running as shown below.
Sample Program to perform Heap Dump Analysis
Launch the above program via Debugger with JvisualVM set as preferred launcher in Debug Configuration as shown earlier. Once the debugger reaches the first break-point. Navigate to monitor Tab as shown below.
Click on Heap Dump button as shown above to obtain the heap-dump and analyse.
Snapshot at Debug point 1 - Memory is not occupied since no object is instantiated yet. The options Summary, Objects, Thread will tell you the amount of space occupied by a Object or number of instances created (i.e count, size), along with the thread state in case they are in locked, sleep or waiting state. Next take the second snapshot at the second debug point.
At Snapshot 2 - Shows the number of objects instantiated and Size occupied. Every Object when created occupies 16 bytes of memory with compressed OOPS as shown. You can refer the link below regarding compressed oops.
Above image shows each instance created has occupied 16 bytes. Based on the amount of memory occupied, we can suspect the Reason for application slowness in the production and If the Retained count is more than zero for any created object that means there is a suspicion for a memory leak.
b) Eclipse Memory Analyser (JMAT):
Open the generated heap-dump via JMAP using JMAT. The Overview gives you an overall picture of the size occupied.
Click on either Histogram or Leak suspects to get more information based on the total number of instances created for a class and the heap size occupied by it.
Now that you know how to use the tools, you might want to use this knowledge further to analyze the memory leak, Click the below link to know more
Analysis of Memory Leak in Java Applications via Heap Dump
If you enjoyed reading it, you can click the like, share button and let others know about it. If you have would like me to add anything else, please feel free to leave a response ??
#heapdump #analysis #Jmap #Jmat #JVisualVM #MemoryLeak #JayReddy