How to capture Heap Dump from app with ADB in Android

You can capture heap dump of a running process with the adb command.

$ adb shell am dumpheap [--user <USER_ID> current] [-n] [-g] <PROCESS> <FILE>
      Dump the heap of a process.  The given <PROCESS> argument may
        be either a process name or pid.  Options are:
      -n: dump native heap instead of managed heap
      -g: force GC before dumping the heap
      --user <USER_ID>

1. Capture Heap Dump with ADB

Check PID of App to extract memory dump

$ adb shell ps -ef | grep com.android.chrome
u0_a124        4779    266 5 22:29:20 ?     00:00:01 com.android.chrome

Capture Heap dump for the pid 4779(Chrome) with adb command. the heap dump file will be saved on /data/local.

$ adb shell am dumpheap 4779
File: /data/local/tmp/heapdump-20210623-223001.prof
Waiting for dump to finish...

Copy files to local PC

$ adb pull /data/local/tmp/heapdump-20210623-223001.prof

Now, you can analyze the extracted prof file using Android Studio, etc.

2. Open the prof file in Eclipse MAT

You can open and analyze the prof file with Eclipse MAT.

But, you need to covert the prof to the MAT format file.

There is a tool called hprof-conv in the SDK that can convert prof files to MAT-readable files with the following command:

$ hprof-conv heapdump-20210623-223001.prof heapdump-20210623-223001.prof.conv

Then, just open the converted file in MAT.

3. where can I find the hprof-conv tool

hprof-conv is also in the sdk platform-tools folder installed when you installed ADB from Android Studio.

../Sdk/platform-tools/$ ls -al

drwxrwxr-x  5 js js    4096 1218  2020 .
drwxrwxr-x 15 js js    4096  416 20:22 ..
-rwxrwxr-x  1 js js 7262264 1218  2020 adb
-rwxrwxr-x  1 js js   13696 1218  2020 hprof-conv
...

Related Posts

codechachaCopyright ©2019 codechacha