Skip to main content

Java Heap Dump Analyzer


Overview

Identifying reason for the out of memory error of java applications with larger heap size may be one of the nightmares of a developer, because most of the out of memory situations may not be identified during the testing phase. It may occur only in production after running for a long time. The purpose of this article is to explain the use of Heap analyser tool to identify the memory leakage of larger enterprise java applications, which uses larger size of heaps. Before going to the details we will discuss about following points.
  •         Heap
  •      Garbage Collection
  •          Out of memory

What is heap?

The space used by the Java Runtime to allocate memory to Objects and JRE Classes is called Heap. The heap space can be configured using the following JVM arguments.
o   -Xmx<size> - Setting maximum Java heap size
o   -Xms<size> - Setting initial Java heap size
The maximum heap size can be configured in a 32 bit JVM is 2GB. If any application requires more than 2 GB, it should run on 64 bit JVM. 64MB is the maximum heap size by default.

Garbage Collection

One of the advantages of Java over C++ is automatic memory management. In C++ memory can be released manually, but it will happen automatically in Java using a process call Garbage collection. Garbage collection will free up the memory of the object that doesn’t have any reference. That is will destroy the unused objects. Garbage collection process can be tune for different applications based on the object creation characteristics of the application. This can be achieved through number of JVM arguments. Following are the few JVM arguments which can be used to tune the garbage collection process.

Option
Description
-XX:-UseParallelGC
Use parallel garbage collection for scavenges.
-XX:-UseParallelOldGC
Use parallel garbage collection for the full collections. Enabling this option automatically sets -XX:+UseParallelGC.
-XX:NewRatio
Ratio of old/new generation sizes. The default value is 2.
-XX:SurvivorRatio
Ratio of eden/survivor space size. The default value is 8.              
-XX:ParallelGCThreads
Sets the number of threads used during parallel phases of the garbage collectors. The default value varies with the platform on which the JVM is running

GC Execution has direct relationship with the size of the Heap.
  • Larger Heap size will increases the GC execution time but decreases the number of GC executions.
  • Smaller Heap Size will increases the number of GC executions but decreases the GC execution time

Out of memory

The java.lang.OutOfMemoryError will occur when the application tries to add more objects into the heap and there is no space left. This will happen when the maximum heap size set in the start of application is filled with the objects and garbage collector is not able free up the memory because the all objects in heap still have some references. This may happen because of two reasons.

  • The application may need more memory to run; because of the currently allocated heap size is not enough to accommodate the objects generated during the run time.
  • The other reason may be due to the coding error the application is keeping the references of unwanted objects


The solution for the first reason is, increase the heap size. But the solution for the second is analyzing the code flow and heap dump to identify the unwanted objects in heap. To analyse the application heap we need to take the heap dump and open the same in memory analyzing tool.

In this article we will discuss on how to take heap dump of an application running on Oracle Java and analyse the same in Eclipse Memory Analyzer.

How to take Heap Dump

Heap dump can be take in two ways,
  • JVM argument can be added to generate heap dump whenever an OutOfMemoryError occurs

-XX:+HeapDumpOnOutOfMemoryError option can be added to generate a heap dump on OutOfMemoryError. By default the heap dump is created in a file called java_pid pid .hprof in the working directory of the VM. But we can set alternative path using the JVM option -XX:HeapDumpPath=path
  •      Using jmap tool available with JDK. Following command can be executed from the command line.
             jmap -dump:format=b,file=heap.bin <pid>
                   <pid> can be replaced with process id of the application

Eclipse Memory Analyzer

Eclipse Memory Analyzer can download from the following location.
Unzip the downloaded file and double click ‘MemoryAnalyzer’ to start the application.
Execute a java program which is continuously running.
Take heap dump using jmap
jmap -dump:format=b,file=heap.bin 6920
Identify the process id of running application in Windows from Task Manager
In Linux to identify the process id use ‘ps –ef | grep java’
Open the heap dump in Eclipse Memory Analyzer using the option File à Open Heap Dump
First it will prompt to create a leak suspect report. User can create or skip the same.
In overview tab of the memory analyser will show the total size of the heap and it will show a pie chart of object wise size.
Click on the highest value in the pie chart and select List Objects à with outgoing references

It will open a new tab ‘List Objects’ and expand the tree structure of the object.
User can expand and find the objects available in heap. In this current example the HeapLoader object consist of a list of School object and school object consist of a list of students.
School List in HeapLoader
Each school object in List and its attributes
Student object in school and its attribute
There is an option available in Eclipse Memory Analyser to Acquire Heap Dump, if the application is running on same machine. Select File  à Acquire Heap Dump option will show the all Java applications available in machine. Select the process, browse the path where the heap want to save and click finish button.
These are the basics steps to analyse a heap dump. There are more options available in Memory Analyser, which will give more insights into the heap dump. And Eclipse Heap Analyser can be used for the dumps generated from the application running on Oracle Java.  To analyse the heap dump running on IBM java can be downloaded from

 Sample application

The sample application used in the above example consists of two objects School and Student.
School will have a list of Students. The application has a list in the main class which will load schools with students. And the application will continue in an infinite loop. The number of schools and students can be given as input from command line

Code is available at:


Download and import the project in Eclipse. To run the application, export the jar from project.
Sample command to run application: java -jar hear-loader.jar 100 100

References


Comments

  1. Thnq For Sharing For Good Info Vasth
    Syngas analyzer for landfill gas and emissions monitoring provide efficient and effective solutions for a broad range of gas measurement applications.

    ReplyDelete
  2. I enjoy what you guys are usually up too. This sort of clever work and coverage! Keep up the wonderful works guys I’ve added you guys to my blog roll.
    Surya Informatics

    ReplyDelete

Post a Comment

Popular posts from this blog

Visual VM tutorial

VisualVM is a visual tool available with JDK to monitor and analyze the performance of Java applications. Accessing Visual VM  In Windows   /bin/jvisualvm.exe  In Linux  /bin/jvisualvm  By default the Locally executing Java applications will be listed under Local node of Visual VM Visual VM – Remote Monitoring The following steps will help the user to monitor the remote application from windows machine Step 1 : Add following in remote application JVM argument -Dcom.sun.management.jmxremote.port=5555 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false Step 2 : Add Remote Host in local Visual VM Step 3: Step 4:   Step 5 : Enter the JMX port given as JVM argument Step 6 : Double click on the newly added node  This will display the JVM details in the right hand side.

How to take thread dump of a running application in Oracle and IBM Java ?

A thread dump will help a developer to identify the application hanging issues and performance issues. To analyze this, please take, a continuous snapshot of thread dumps and analyze using a thread dump analyzing tool. In Oracle Java you can use jstack tool. This executable is available in the JAVA_HOME/bin folder. Use the following command ' jstack -l <PID> >> threaddump ' . This will write the thread dump into a file 'threaddump' in the directory from you have executed the command. <PID> should replaced with actual process id. You can use the following command to find the process id in Unix. ' ps -ef | grep java ' . In IBM Java you can use ' kill -3 <PID> ' . This will write the thread dump in application running console or in nohup file or a thread dump file will be generated in the program running directory.