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
  • Analyzing “java.lang.OutOfMemoryError: Failed to create a thread” Error
  • Fixing OutOfMemoryErrors in Java Applications
  • Ulyp: Recording Java Execution Flow for Faster Debugging

Trending

  • Java’s Next Act: Native Speed for a Cloud-Native World
  • A Guide to Developing Large Language Models Part 1: Pretraining
  • It’s Not About Control — It’s About Collaboration Between Architecture and Security
  • While Performing Dependency Selection, I Avoid the Loss Of Sleep From Node.js Libraries' Dangers
  1. DZone
  2. Coding
  3. Java
  4. How To Capture Java Heap Dumps (8 Options)

How To Capture Java Heap Dumps (8 Options)

Is your Java application's memory out of whack? Depending on the error, at least one of these industry solutions should help.

By 
Ram Lakshmanan user avatar
Ram Lakshmanan
DZone Core CORE ·
Updated Jun. 21, 22 · Tutorial
Likes (21)
Comment
Save
Tweet
Share
427.8K Views

Join the DZone community and get the full member experience.

Join For Free

Heap Dumps are vital artifacts to diagnose memory-related problems such as slow memory leaks, Garbage Collection problems, and java.lang.OutOfMemoryError.They are also vital artifacts to optimize memory consumption.

There are great tools like Eclipse MAT and Heap Hero to analyze heap dumps. However, you need to provide these tools with heap dumps captured in the correct format and at the correct point in time.

This article gives you multiple options to capture heap dumps. However, in my opinion, the first three are effective options to use, and others are good options to be aware of.

1. yCrash Open Source Script

The yCrash script is a powerful open-source script that captures not only heap dump, but also 16 essential artifacts from your application to troubleshoot performance problems. Here’s how you can utilize the yCrash script to capture heap dump and more:

  1. Download the latest yc-data-script from this location
  2. Unzip the downloaded yc-agent-latest.zip file. (Say you are unzipping in /opt/workspace/yc-agent-latest folder)
  3. In the unzipped folder, you will find yc-data-scriptby operating system:
    • linux/yc – If you are running on Unix/Linux, then use this script.
    • windows/yc.exe – If you are running on Windows, then use this script.
    • mac/yc – If you are running on MAC, then use this script.
  4. You can execute the ycscript by issuing the following command:
./yc -j {JAVA_HOME} -onlyCapture -p {PID} -hd 

where 
JAVA_HOME is the home directory where JDK is installed 
PID is the troubled target process ID


Example:

./yc -j /usr/java/jdk1.8.0_141 -onlyCapture -p 15326 -hd


When you execute the command, yCrash script will capture all the application-level and system-level artifacts/logs from the server from the target application for analysis. Captured artifacts will be compressed into a zip file and stored in the current directory where the above command was executed. The zip file will have the name in the format: ‘yc-YYYY-MM-DDTHH-mm- ss.zip.’ 

Example: ‘yc-2021-03 06T14-02-42.zip’.

For more details, visit it’s GitHub repository.

2. jmap

jmap prints heap dumps into a specified file location. This tool is packaged within JDK. It can be found in \bin folder.

Here is how you should invoke jmap:


jmap -dump:format=b, file=<file-path> <pid> 

where
pid: is the Java Process Id, whose heap dump should be captured
file-path: is the file path where heap dump will be written in to.


Example:

    jmap -dump:format=b,file=/opt/tmp/heapdump.bin 37320


Note: It’s quite important to pass the “live” option. If this option is passed, then only live objects in the memory are written into the heap dump file. If this option is not passed, all the objects, even the ones that are ready to be garbage collected, are printed in the heap dump file. It will increase the heap dump file size significantly. It will also make the analysis tedious. To troubleshoot memory problems or optimize memory, just the “live” option should suffice the need.

3. HeapDumpOnOutOfMemoryError

When an application experiences java.lang.OutOfMemoryError, it’s ideal for capturing heap dump right at that point to diagnose the problem because you want to know what objects were sitting in memory and what percentage of memory they were occupying when java.lang.OutOfMemoryError occurred. However, due to the heat of the moment, most times, IT/Operations team forgets to capture heap dump. Not only that, they also restart the application. It’s extremely hard to diagnose any memory problems without capturing heap dumps at the right time.

That’s where this option comes in very handy. When you pass -XX:+HeapDumpOnOutOfMemoryError system property during application startup, JVM will capture heap dumps right at the point when JVM experiences OutOfMemoryError.

Sample Usage:

-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/opt/tmp/heapdump.bin


Note: Captured heap dump will be printed at the location specified by -XX:HeapDumpPath system property.

E-Creativity Best Practice: Keep this property configured in all the applications at all times, as you never know when OutOfMemoryError will happen.

4. jcmd

jcmd tool is used to send diagnostic command requests to the JVM. It’s packaged as part of JDK. It can be found in\bin folder.

Here is how you should invoke jcmd:

jcmd  GC.heap_dump  
where
pid: is the Java Process Id, whose heap dump should be captured
file-path: is the file path where heap dump will be written in to.


Example:

1  jcmd 37320 GC.heap_dump /opt/tmp/heapdump.bin


5. JVisualVM

JVisualVM is a monitoring and troubleshooting tool that is packaged within the JDK. When you launch this tool, you can see all the Java processes that are running on the local machine. You can also connect to the Java process running on a remote machine using this tool.

Steps:

  1. Launch jvisualvm under \bin\ folder
  2. Right-click on one of the Java process
  3. Click on the ‘Heap Dump’ option on the drop-down menu
  4. A heap dump will be generated
  5. File path where the heap dump is generated will be specified in the Summary Tab > Basic Info > File sectionFig: Capturing Heap Dump from JVisualVM

Fig: Capturing Heap Dump from JVisualVM

6. JMX

There is a com.sun.management:type=HotSpotDiagnostic MBean. This MBean has dumpHeap operation. Invoking this operation will capture the heap dump. dumpHeap operation takes two input parameters:

  1. outputFile: File path where heap dump should be written
  2. live: When true is passed, only live objects in the heap are captured

You can use JMX clients such as JConsole, jmxsh, Java Mission Control to invoke this MBean operation.Fig: Using Java Mission Control as the JMX client to generate heap dump

Fig: Using Java Mission Control as the JMX client to generate a heap dump

7. Programmatic Approach

Instead of using tools, you can also programmatically capture heap dumps from the application. There might be cases where you want to capture heap dumps based on certain events in the application. Here is a good article from Oracle that gives the source code for capturing heap dumps from the application by invoking the com.sun.management:type=HotSpotDiagnostic MBean JMX Bean, which we discussed in the above approach.

8. IBM Administrative Console

If your application is running on IBM Websphere Application Server, you can use the administrative console to generate heaps.

Steps:

  1. Start administrative console
  2. In the navigation pane, click Troubleshooting > Java dumps and cores
  3. Select the server_name for which you want to generate the heap dump
  4. Click Heap Dump to generate the heap dump for your specified server

You can also use wsadmin to generate heap dumps.

Additional Knowledge

Java Heap Stack

Java (programming language) Tool Memory (storage engine)

Published at DZone with permission of Ram Lakshmanan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Understanding Root Causes of Out of Memory (OOM) Issues in Java Containers
  • Analyzing “java.lang.OutOfMemoryError: Failed to create a thread” Error
  • Fixing OutOfMemoryErrors in Java Applications
  • Ulyp: Recording Java Execution Flow for Faster Debugging

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!