How do I analyze a Java heap dump?
Environment
- Java
Issue
- How do I analyze a heap dump?
Resolution
Disclaimer: The following information has been provided by Red Hat, but is outside the scope of the posted This content is not included.Service Level Agreements and This content is not included.Scope of Coverage. Red Hat does not support software contained in the EPEL repository, or other third-party repositories, or from third-party non-Red Hat sources. The information is provided as-is without representations or warranties about the suitability or accuracy of the information provided. The intent is to provide information to accomplish the system's needs. Use of the information below is at the user's own risk.
Links contained herein to external website(s) are provided for convenience only. Red Hat has not reviewed the links and is not responsible for the content or its availability. The inclusion of any link to an external website does not imply endorsement by Red Hat of the website or their entities, products or services. You agree that Red Hat is not responsible or liable for any loss or expenses that may result due to your use of (or reliance on) the external site or content.
Tools
OpenJDK, Sun, HP JDK
These JVMs create binary hprof heap dumps. The hprof format is platform independent, so it isn't necessary to analyze the dump on the same system that produced it. Tools such as YourKit, NetBeans, HAT, jhat, etc. can analyze hprof format heap dumps; however, the Content from www.eclipse.org is not included.Eclipse Memory Analyzer (MAT) tool is the preferred tool of Red Hat support and has excellent Content from wiki.eclipse.org is not included.documentation.
The Content from www.ibm.com is not included.IBM HeapAnalyzer tool is able to parse hprof format heap dumps; however, it is not recommended to use for analysis, as it is no longer being maintained and has known issues. See IBM HeapAnalyzer Inconsistencies with Eclipse Memory Analyzer (MAT) Tool.
IBM JDK
The IBM JVM produces a heap dump in the proprietary phd (portable heap dump) format. The Eclipse Memory Analyzer tool is able to parse phd format heap dumps and is the recommended tool for analysis, not the Content from www.ibm.com is not included.IBM HeapAnalyzer tool, as the IBM HeapAnalyzer tool is no longer being maintained.
Analysis
Sometimes the cause of the retention is obvious, and other times it takes an understanding of the code and the circumstances that caused the OutOfMemoryError to know whether the issue is a leak or the heap is simply not large enough.
There are generally three types of retention points:
- A single object retaining memory.
- Many small objects retaining memory.
- Retention associated with a thread.
If a single object retention point is not found, group by class to see if many small objects are retaining a lot of memory.
Check if the [biggest retainer is a thread]. Is the heap dump created when OutOfMemory happened much much smaller than the Xmx max heap size? This is a sign of thread retention. For example: HornetQ streaming big StreamMessages causes 'OutOfMemoryError: Java heap space' .
When the retention point is identified, the paths from GC roots can be viewed to see what is keeping the object(s) alive.
One technique to make memory leaks more visible and detectable is to double the normal maximum heap size. When OutOfMemoryError occurs, the size of the objects related to the leak will be about half the size of the heap.
Unreachable objects
By default, Eclipse MAT strips out unreachable objects when creating its index. If you have a problem where there is a lot of object allocation causing excessive GC, and you want to find out what objects are being allocated and freed, you will want them still in the heap dump.
To have unreachable objects be available, you need to manually run the index step from the command line (which is usually run automatically when you first load a dump in the UI). You need to run:
MemoryAnalyzer -consolelog -keep_unreachable_objects -application org.eclipse.mat.api.parse myheaddump.hprof
If you then load the heap dump into MAT normally, you will have access to the unreachable objects, however, you can no longer run the "Unreachable object histogram". You can also only tell which objects are reachable and which are not by looking at their path to a GC Root - unreachable ones will have an unknown GC root.
We have seen cases where the GC logging shows no space able to be reclaimed, yet Eclipse Memory Analyzer Tool (MAT) analysis of a corresponding heap dump shows a large amount of retention as unreachable objects. This may be a MAT bug, as it is clear from the GC logging that objects are not able to be reclaimed. In those cases, it is useful to parse the heap dump with unreachable objects included.
We have seen cases when MAT hangs trying to open a heap dump. Try enabling "Keep unreachable objects" under Window > Preferences> Memory Analyzer. Presumably, there is significant overhead to MAT stripping out unreachable objects.
Special Information
It is possible to find a lot of special information from a heap dump, by looking at certain objects.
- JVM arguments
- Thread dump
- Running Hibernate query for a large session
- Objects awaiting finalisation
- Finding the contents of arrays and collections using OQL
- Check the datasource pool configuration and preparedStatement
- Content from eclipsesource.com is not included.Useful tips for using Eclipse MAT
- Heap dump analysis in a secure environment
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.