If your Java application experiences OutOfMemoryError, memory leak or any memory related problems, Java Heap Dump analysis will become your ultimate resolve. It facilitates you to answer hard questions such as: What’s consuming memory? Do you have leaks? And why isn’t garbage collection cleaning things up? In this post let’s learn effective strategies and tips to generate heap dump and analyze.
What is a Java heap dump and why is analyzing it important?
A Java Heap Dump is like a photograph of your application’s memory. It contains information about objects present in memory, how much size they occupy, who is referencing them and who they are referencing to, partial information about threads that are running in the application and system properties of the application. It helps to answer questions such as:
- What objects are using the most memory?
- Are there duplicate or unnecessary objects?
- Are objects being retained that should’ve been garbage collected?
- Are there any memory leaks?
- What caused an OutOfMemoryError?
Analyzing a heap dump gives you direct insight into memory problems. Instead of guessing, you can see exactly what was in memory and uncover the root causes of performance issues and crashes.
How can I generate a Java heap dump? What are the different methods?
There are 8 different options to generate heap dump. In nutshell they are:
1. yc-360 Open-Source Script: A command-line tool that captures a heap dump and 16 additional artifacts (logs, thread dumps, GC info) that facilitates comprehensive troubleshooting. For details on how to trigger visit the yc-360 Script GitHub page.
2. Jmap is a JDK tool that writes a heap snapshot (in HPROF format) of a running Java process directly to a file. It can be triggered like this:
jmap -dump:format=b[,live],file=<path> <pid>
3. -XX:+HeapDumpOnOutOfMemoryError is a JVM argument that can be passed to your application like this:
-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=<file>
Whenever your application encounters an OutOfMemoryError, this argument triggers a heap dump and writes it to disk.
4. jcmd is a JDK utility that can send commands to a running JVM. It can also be used to generate a heap dump by issuing the command:
jcmd <pid> GC.heap_dump <file-path>
5. JVisualVM is a GUI tool bundled with the JDK to monitor JVM processes and manually generate heap dumps. Connect jvisualvm to your application and then click on the “Heap Dump” from its context menu.
6. JMX (HotSpotDiagnostic MBean): You can trigger a heap dump by calling the “dumpHeap(outputFile, live)” operation on the “HotSpotDiagnostic” MBean via JMX. This can be done using tools like JConsole, Java Mission Control, or a custom script.
7. Programmatic Approach: You can leverage the “HotSpotDiagnostic” MBean in your Java code to capture heap dumps at custom points. It’s ideal for integrating dumps into your application’s internal monitoring logic .
8. Application Server: Some application servers, like IBM WebSphere, provide built-in options to capture heap dumps from their admin consoles.For example, in WebSphere, you can go to “Troubleshooting → Java dumps and cores” to generate heap dumps. It also offers a command-line option using the wsadmin scripting tool.
To learn more about these options in detail, you may visit this blog post: HOW TO CAPTURE JAVA HEAP DUMPS?
What tools can I use to analyze a Java heap dump?
Here are some of the popular heap dump analysis tools. You can use the one of your choice:
- HeapHero: A powerful, on-prem and cloud-based heap dump analyzer that uses machine learning to detect memory leaks, visualize object graphs, and deliver easy-to-understand, shareable reports with actionable insights.
- Eclipse MAT: An open-source desktop tool from Eclipse that provides detailed heap dump exploration using dominator trees, leak suspects, and object graphs.
- VisualVM: A lightweight monitoring and profiling tool bundled with the JDK, useful for on-the-fly heap inspection and basic memory analysis.
- JProfiler: A commercial profiling tool offering advanced memory and performance diagnostics, with deep integration into live applications.
- YourKit: A professional Java profiler that provides detailed memory analysis, object retention graphs, and snapshot comparison features for identifying memory issues.
What is HeapHero and how can it help in heap dump analysis?
HeapHero is a heap dump analyzer that helps you quickly identify memory issues in Java and Android apps. Here’s how it can help:
1. Automatic leak detection & wasted-memory insights: HeapHero analyzes heap dumps and highlights memory leaks, inefficient data structures, duplicate objects/strings, and calculates how much memory is actually wasted
2. Intuitive, shareable reports with collaboration support: You get a polished one page dashboard with Dominator Tree, Class Histogram, large-object views, and it even provides shareable links and integrates with tools like JIRA and Grafana
3. Seamless usage via Web, REST API: You can drag and drop your .hprof files via the web UI or invoke its REST API to analyze heap dumps programmatically.
4. Machine learning powered recommendations: HeapHero uses ML to automatically flag suspects, such as surprisingly large object graphs or excessive duplicates.
5. Security: Since heap dump contains sensitive information such as Credit Card numbers, SSN, email addresses… so engineers who have access to the heap dump can see this sensitive information. HeapHero provides HeapDump Sanitization capability, such that all the raw data in the heap dump can be removed. It makes Heap Dump analysis more secure.
You can learn more about HeapHero’s capabilities from here.
Typical Workflow for Analyzing a Heap Dump using a Tool
One of the best things about HeapHero is how easy and intuitive it makes heap dump analysis, even for complex memory issues. Here’s a step-by-step walkthrough of what a typical workflow looks like:
1. Upload Your Heap Dump file into the HeapHero tool or if you’re automating the analysis, use its REST API. Whether it’s a Java or Android heap dump, HeapHero will parse your heap dump and provide you with a detailed, interactive report.
2. Review ML-Based Problem Detection: HeapHero uses built-in machine learning to scan your heap dump for common issues like memory leaks, duplicate objects, inefficient data structures, and more. These issues are reported right at the top of your report. Just click any of them to get a closer look: you’ll see which classes or objects are involved, how much memory they’re using, and what keeps them alive. This will point you to the memory leak origination source.
3. View High-Level Metrics: Before getting deep into the reports, take a quick glance at the summary metrics:
- Total memory usage
- Object count
- Number of duplicate strings
- Leak suspects
This gives you a high-level understanding of what’s going on under the hood.
4. Take a closer look at the Dominator Tree: Next go to the Dominator Tree section, which is the most important section in the report. In this section you will see which objects are holding on to the most memory (retained size). Always objects which are holding most of memory are responsible for memory leak. Thus investigate the top nodes in this section. Use the Incoming and Outgoing References to identify who is keeping these objects alive and what objects contain which is causing the memory leak.
5. Use Specialized Views: Dominator Tree should be able to resolve most of the problems. For certain rare special problems you need to use following special views:
- ‘Class Histogram’ for a sorted breakdown of object counts by class. If a particular class is creating too many instances, this section will be helpful to spot it.
- ‘Duplicate Classes’ to isolate class loading leaks
- ‘OQL’ (Object Query Language) to query the objects/classes in the heap dump
- ‘Threads’ to review the leaks caused by the threads running in the application
- GC Roots to trace memory chains
- Unreachable Objects for spotting memory waste
These views can help you narrow down specific pain points without having to sift through everything manually.
6. Share and Collaborate: Once your report is ready, you can generate a secure, shareable link for your teammates. And if you’re working in a security-sensitive environment, HeapHero also offers on-premise deployments, so your data never leaves your firewall.
Conclusion
Whether you are trying to isolate memory leaks or reduce memory consumption, heap dumps give you hard evidence not just symptoms. With the right tools like HeapHero and a structured workflow, you can diagnose issues faster and improve your application’s stability and performance.

Share your Thoughts!