How to Diagnose Memory Leaks

Memory leaks don’t have to be hard/scary/tedious problems to solve if you follow the three simple steps mentioned below:

Step 1: Capture baseline heap dump

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

jmap -dump:format=b,file= 

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, there 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 plays 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 a 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 following JVM property:

-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=

file-path: is the file path to which the heap dump will be written.

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

Step 3: Compare heap dumps

Objects that 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 that are causing memory leak.

You can consider using a heap dump analyzer tool such as HeapHero.io for this purpose. When you load the heap dumps in to heapHero.io, it provides rich information about your application’s memory. There is a “Large Objects” section, that reports the 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 is causing memory leak in your application. You can also click on any of the largest object to see the children, grandchildren, great grandchildren objects present in it.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may