Tip: Options for Capturing Heap Dump from an Android App

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

In this article, we have given few different options to capture Heap Dumps from Android Apps. Once you have captured heap dumps, you can use great tools like HeapHero and Android studio?s heap analyzer to analyze heap dumps.

1. Memory Profiler Below are the steps to capture heap dumps from Memory Profiler in Android studio:

a. Run the app and select the device you want to profile from Android Studio.

b. In your Android studio, click on View >> Tool Windows >> Android Profiler

Fig 1: Select Android Profiler

c. There will be Memory timeline, which would be below the CPU timeline, but above the Network timeline. In this memory timeline, click on download button (highlighted in the below image) to generate heap dump from the Android app.

Fig 2: Generate Android Heap Dump

d. To store heap dump in your system, click on the highlighted icon in the below image.

Fig 3: Save the generated Android Heap Dump

e. Choose a location to save the generated heap dump file.

Fig 4: Select the file location to save the generated Android heap dump file

2. Android Debug Bridge (ADB)

Android Debug Bridge is a command line tool which allows you to interact with a device. ADB provides a variety of device actions, such as installing and debugging apps. It also gives access to the Unix shell to run a variety of commands on the device. You can use this tool to generate android heap dumps. Launch ADB shell and follow the below steps:

a. Identify your Android App?s Process Id First step is to identify your Android App?s process Id. You can do that by issuing below command:

adb shell ps | grep 

The command above will return details about the process. The second number will be the PID of your app. Please check the below screenshot.

Fig 5: adb shell to get android process id

b. Create a Heap Dump:

adb shell am dumpheap    PID: Your Android App Process IdHEAP-DUMP-FILE-PATH: Location where heap dump file should be generated


adb shell am dumpheap 1769 /data/local/tmp/android.hprof

c. Pull the file to your machine Above step will generate the heap dump file in the device. For analysis, you need to pull the generated file to your machine. You do that by issuing below command:

adb pull HEAP-DUMP-FILE-PATH: Location where heap dump file


adb pull /data/local/tmp/android.hprof

3. Capture Heap Dumps on OutOfMemoryError

If you place the below code in your application, it will capture heap dumps whenever your application receives an OutOfMemoryError.

public class CaptureHeapDumps extends Application {       private static final String FILE_NAME = "heap-dump.hprof";       @Override       public void onCreate() {          super.onCreate();Thread.currentThread().setUncaughtExceptionHandler(OutOfMemoryException());       }       @NonNull       private Thread.UncaughtExceptionHandler OutOfMemoryException() {               return new Thread.UncaughtExceptionHandler() {               @Override               public void uncaughtException(Thread t, Throwable e) {                  String directory = getApplicationInfo().dataDir;                  String absolutePath = new File(directory, FILE_NAME)                         .getAbsolutePath();                  try {                      Debug.dumpHprofData(absolutePath);                  } catch (IOException e) {                      e.printStackTrace();                  }               }        };    }}

This would generate the heap dump file in this location: /data/user/0/appname/heap-dump.hprof

Author bio: Ram Lakshmanan


Every single day, millions & millions of people in North America???bank, travel, and commerce???use the applications that Ram Lakshmanan has architected. Ram is an acclaimed speaker in major conferences on scalability, availability, and performance topics. Recently, he has founded a startup, which specializes in troubleshooting performance problems.

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


The Latest

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

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.