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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • All You Need To Know About Garbage Collection in Java
  • Java Memory Management
  • Introduction Garbage Collection Java
  • Troubleshooting Memory Leaks With Heap Profilers

Trending

  • Java 8 Optional - Avoid Null and NullPointerException Altogether - and Keep It Pretty
  • When One Docker Swarm Replica Became Unreachable Over an Overlay Network: A Production Case With Traefik
  • AI-Driven Integration in Large-Scale Agile Environments
  • What Cloud Engineers Actually Need to Know About AI Infrastructure

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. How to Diagnose Memory Leaks

How to Diagnose Memory Leaks

Performance of your Java application is dependent on the stability of your memory, so click more to learn more about keeping your memory secure.

By 
Ram Lakshmanan
DZone Core CORE ·
Apr. 02, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
43.1K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Memory leaks don’t have to be a hard/scary/tedious problem to solve if we can follow below mentioned 3 simple steps:

Step 1: Capture Baseline Heap Dump

You need to capture heap dump when it’s in the healthy state. Start your application. Let it take real traffic for 10 minutes. At this point, capture heap dump. Heap Dump is basically the snapshot of your memory. It contains all objects that are residing in the memory, values stored in those objects, inbound & outbound references of those object. You can capture Heap dump using the following command:

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.

If you don’t want to use jmap for capturing heap dumps, here are several other options to capture heap dumps.

It’s always better to capture heap dump in the production environment (unless in the test environment, you can mirror the exact production traffic pattern). Traffic type and its volume play a primary role in the type and number of objects created in the memory.

Step 2: Capture Troubled Heap Dump

After doing step #1, let the application run. Before the application crashes, take another heap dump once again. Often times, it might be challenging to capture heap dumps before it crashes because we don’t know when the application will crash. Is it after 30 minutes, 3 hours, 3 days? Thus, it’s ideal to start your application with the following JVM property:

-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=<file-path>
file-path: is the file path where heap dump will be written in to.

This property will trigger heap dump right when the application experiences OutOfMemoryError.

Step 3: Compare Heap Dumps

Objects which are causing memory leaks grow over the period. If you can the identify objects whose size has grown between the heap dumps captured in step #1 and step #2, then those are the objects which are causing memory leak.

daignose-memory-leak-1








Fig 1: Largest Objects reported by HeapHero.io

daignose-memory-leak-2















Fig 2: Object Tree showing children, grandchildren, great-grandchildren

You can consider using heap dump analyzer tool such as HeapHero.io for this purpose. When you load the heap dumps into HeapHero.io, it provides rich information about your application’s memory. There is a “Large Objects” section (shown in Fig 1), which reports largest objects that are sitting in the memory. Compare this section between heap dumps captured in step #1 and step #2. If you notice any abnormal growth of objects, then they are the ones which are causing memory leak in your application.  You can also click on any of the largest objects to see the children, grandchildren, great-grandchildren objects present in it. Screen shot of this section is shown in Fig #2.



Memory (storage engine) garbage collection Dump (program) Object (computer science) application

Opinions expressed by DZone contributors are their own.

Related

  • All You Need To Know About Garbage Collection in Java
  • Java Memory Management
  • Introduction Garbage Collection Java
  • Troubleshooting Memory Leaks With Heap Profilers

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook