DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Understanding Root Causes of Out of Memory (OOM) Issues in Java Containers
  • All You Need To Know About Garbage Collection in Java
  • Difference Between InitialRAMPercentage, MinRAMPercentage, MaxRAMPercentage
  • JVM Tuning Using jcmd

Trending

  • How to Format Articles for DZone
  • Develop a Reverse Proxy With Caching in Go
  • Enhancing Security With ZTNA in Hybrid and Multi-Cloud Deployments
  • Fixing Common Oracle Database Problems
  1. DZone
  2. Coding
  3. Java
  4. Using Heap Dumps to Find Memory Leaks

Using Heap Dumps to Find Memory Leaks

This guide covers heap dump collection methods, memory leak detection strategies, JVM memory warning signs, and best practices for Java memory optimization.

By 
Pratik Dwivedi user avatar
Pratik Dwivedi
·
Mar. 27, 25 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
6.8K Views

Join the DZone community and get the full member experience.

Join For Free

A JVM (Java Virtual Machine) runs programs in Java and many other languages and provides the runtime environment, CPU, memory, and security management. At any particular moment, the running JVM has many objects, and a heap dump is a snapshot of the memory allocated to all these objects. 

In this post, we will discuss how to use heap dumps to find memory leaks. 

What Is a Heap Dump?

A heap dump is like a blueprint of how the memory is used and who occupies what percentage of memory. Heap dumps are crucial in optimizing memory usage in Java applications, as well as for detecting and troubleshooting memory-leak problems. 

The JVM allocates all the classes and objects needed for program execution memory on the heap. As the program progresses, some objects are no longer needed (no reference to them exists), and the garbage collector reclaims the memory allocated to them. This reclaimed memory is made available for newer objects needed by the programs running inside the JVM. 

How to Take a Heap Dump?

A heap dump can be taken by simple Java commands, either on the command line or via runtime options. 

1. Via the JMAP Command

The jmap command takes as input a process ID, and prints memory statistics like: 

  • The class loader statistics of Java heap
  • Heap usage statistics like total capacity/available/free memory 
  • Information on objects awaiting finalization 
  • Histogram of the Java object heap  
  • Garbage collection (GC) algorithm being used
  • Dumps the Java heap in hprof binary format, etc.

Its format is jmap [-clstats/-heap/-finalizerinfo/-histo{:live}/-dump:dump_options] ProcessID. 

Plain Text
 
jmap -heap 12345 
jmap -dump:live,format=b,file=myFileName.hprof 12345  
jmap -histo 12345 


2. Use JVM options

Clearly specify that heap dumps should be taken, especially in case of an OutOfMemoryError.

Java
 
-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/myfolder/myFile.hprof  


3. Use the yCrash Open-Source Tool  

It captures more than 16 essential artifacts, including a heap dump, to take a 360-degree snapshot of your running Java applications. 

4.  Use the JCMD Utility 

JCMD is packaged with your Java installation; its general format is jcmd <PID> GC.heap_dump heapdump.hprof.

Java
 
jcmd 12345 Dump.heap myHeapDumpFileName 


How to Analyze Heap Dumps to Find Memory Leaks

To browse and view the heap dumps visually, you can use the Java VisualVM tool or the more versatile HeapHero tool. I prefer using HeapHero as it is powerful, can pinpoint problems, provides details about memory usage and problems, and provides charts/visualizations that are easily discernible. 

Also, HeapHero's machine learning algorithms give you deep insights into problems and provide accurate solutions/recommendations. Fig 1. Source: Ycrash

Source: yCrash

These are some telltale signs that indicate that something is wrong.

  1. Many duplicate variables contain the same value, e.g., many String objects contain exactly the same string. 
  2. A few objects continuously grow over time, occupying increasing volumes of memory. 
  3. After a certain period, repeated runs of the GC are not able to free up enough memory ( or the free memory keeps decreasing as GC runs keep increasing in frequency).
  4. Objects that could have been singletons are instance variables and have multiple copies in the heap.  
  5. There are a lot of unreachable objects, i.e., objects that exist in the memory but do not have a useful reference or the reference is from another unreachable object. 

Source:Source: Ycrash

Source: yCrash

These unreachable objects must be closed to reclaim memory. Find the redundant/useless copies and use appropriate data types instead of fixed byte arrays/strings, etc. More such techniques can be found here: Fixing out-of-memory errors in Java applications.  

Check the list of the largest objects:

Source: Ycrash

Source: yCrash

HeapHero shows you a list of the largest objects, sorted in descending order. You can also group them by Class/Object/Class Loader/Package. Find all the objects/classes occupying a lot of space and something that can be done about them. For example, closing file/database connections, making singleton objects, or sharing the same String values via static variables (not per instance).

Memory Optimization Maneuver and Typical Use Cases

  1. Identify objects unnecessarily holding references and close them.
  2. Understand the memory usage patterns of your program(s), allocate appropriate initial memory, and apply effective garbage collection strategies. For example:
    • The Parallel Garbage Collector uses multiple threads to collect garbage in the young generation, improving throughput for CPU-bound applications.
    • The Shenandoah Garbage Collector uses a barrier-based approach to minimize pause times and is suitable for large heaps.

To conclude, we have discussed how details hidden inside heap dumps can be used to mitigate the risk of OutOfMemoryErrors and close potential memory leaks.

Your suggestions and comments are welcome. Thanks for your time.

Java virtual machine garbage collection Memory (storage engine)

Opinions expressed by DZone contributors are their own.

Related

  • Understanding Root Causes of Out of Memory (OOM) Issues in Java Containers
  • All You Need To Know About Garbage Collection in Java
  • Difference Between InitialRAMPercentage, MinRAMPercentage, MaxRAMPercentage
  • JVM Tuning Using jcmd

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!