Heap dump generation & Analysis using JMAP, JMAT, JvisualVM Tools

Heap dump generation & Analysis using JMAP, JMAT, JvisualVM Tools

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.

No alt text provided for this image

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

  1. 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.

No alt text provided for this image

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.

No alt text provided for this image

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

No alt text provided for this image

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.

No alt text provided for this image
No alt text provided for this image

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

No alt text provided for this image

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.

No alt text provided for this image

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.

No alt text provided for this image

Once you click on OK and apply. The debugger will automatically launch the VisualVM with the application process running as shown below.

No alt text provided for this image

Sample Program to perform Heap Dump Analysis

No alt text provided for this image

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.

No alt text provided for this image

Click on Heap Dump button as shown above to obtain the heap-dump and analyse.

No alt text provided for this image

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.

No alt text provided for this image

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.

No alt text provided for this image

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.

No alt text provided for this image

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.

No alt text provided for this image

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

要查看或添加评论,请登录

Jayvardhan Reddy Vanchireddy的更多文章

  • Apache Spark-3.0 Sneek peak

    Apache Spark-3.0 Sneek peak

    Apache Spark has remained strong over the years and now is coming back with one of its major releases with its ongoing…

    14 条评论
  • Working of Sqoop Incremental Load

    Working of Sqoop Incremental Load

    In my series of BigData Architecture, we have seen the internal working of Sqoop. Now as part of this article, we'll…

    3 条评论
  • Deep-dive into Spark Internals & Architecture

    Deep-dive into Spark Internals & Architecture

    Apache Spark is an open-source distributed general-purpose cluster-computing framework. A spark application is a JVM…

    12 条评论
  • Sqoop Architecture in Depth

    Sqoop Architecture in Depth

    Apache Sqoop is a data ingestion tool designed for efficiently transferring bulk data between Apache Hadoop and…

    9 条评论
  • HDFS Architecture in Depth

    HDFS Architecture in Depth

    Hadoop consists of mainly two main core components HDFS, MapReduce. HDFS is the Hadoop Distributed File System ( HDFS )…

    3 条评论
  • Hive Architecture in?Depth

    Hive Architecture in?Depth

    Apache Hive is an ETL and Data warehousing tool built on top of Hadoop for data summarization, analysis and querying of…

  • Application Development: 4 Simple Steps to Resolve Remote Debugging Connection Problems

    Application Development: 4 Simple Steps to Resolve Remote Debugging Connection Problems

    As a developer, we frequently debug the application during the development activities. The real time applications are…

    1 条评论
  • 5 Useful Tools for a Full-stack Developer

    5 Useful Tools for a Full-stack Developer

    The below tools will help you increase your productivity and reduce compilation issues on running a debug job…

    1 条评论
  • Database Transaction Leak in Java Application

    Database Transaction Leak in Java Application

    In a real time application the Database leak occurs due to Unclosed transactions created by the programmers. The stakes…

    1 条评论
  • Analysis of Memory Leak in Java Applications via Heap?Dump

    Analysis of Memory Leak in Java Applications via Heap?Dump

    Memory plays a vital role in any application performance and we cannot afford to waste the resources unnecessarily, as…

    6 条评论

社区洞察

其他会员也浏览了