Home » Android » Android Core » How to capture Heap Dumps from Android App? – 3 Options

About Ayusch Jain

Ayusch Jain
Ayusch is a Software Engineer currently working in Android Development. He's worked long enough that he's transformed into an Android himself :P. Additionally, he also maintains a community of Android developers called: AndroidVille and writes about Android on his website: https://ayusch.com

How to capture Heap Dumps from Android App? – 3 Options

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.

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

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.

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

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

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:

1
adb shell ps | grep <APP-NAME>

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

b. Create a Heap Dump:

1
2
3
4
adb shell am dumpheap <PID> <HEAP-DUMP-FILE-PATH> 
 
PID: Your Android App Process Id
HEAP-DUMP-FILE-PATH: Location where heap dump file should be generated

Example:

1
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:

1
2
3
adb pull <HEAP-DUMP-FILE-PATH>
 
HEAP-DUMP-FILE-PATH: Location where heap dump file

Example:

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

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.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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

Published on Java Code Geeks with permission by Ayusch Jain, partner at our JCG program. See the original article here: How to capture Heap Dumps from Android App? – 3 Options

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of