Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to trigger an Android heap dump from the command line?

I would like to be able to trigger an Android heap dump from the command line. Is there a command for that?

Specifically, from the command line, not via Montior or DDMS GUIs

Maybe something like using ddms or adb, e.g. ddms -head-dump or adb shell heapdump? AFAICT monitor and ddms always start in GUI mode, and adb doesn't have a heap dump command.

Update: I tried this, it looked promising, but it doesn't work:

  1. adb jdwp
  2. adb forward tcp:8000 jdwp:1234 (substitute output of 1 for 1234)
  3. jmap -dump:format=b,file=heapdump.hprof localhost:8000

But even the heap summary fails:

jmap -heap localhost:8000
Attaching to remote server localhost:8000, please wait...
Error attaching to remote server: java.rmi.ConnectIOException: error during JRMP connection establishment; nested exception is: 
        java.net.SocketTimeoutException: Read timed out
like image 599
nmr Avatar asked Sep 11 '14 19:09

nmr


People also ask

How do you get heap dump on OutOfMemoryError?

They are very useful to troubleshoot memory-leak problems and optimize memory usage in Java applications. In order to capture a heap dump automatically, we need to add the HeapDumpOnOutOfMemoryError command-line option that generates a heap dump when a java. lang. OutOfMemoryError is thrown.

Does heap dump trigger GC?

It will also trigger a full GC.


2 Answers

In Android pre 3.0 you can use so called

kill -10 <pid> (more)

In Android 3.0 a new command-line tool has been added:

adb shell am dumpheap <pid> <output-file-name>; (more)

Detailed description

To get HPROF you need also change the format of it using hprof-conv

like image 96
Damian Leszczyński - Vash Avatar answered Oct 02 '22 19:10

Damian Leszczyński - Vash


The easy way:

adb shell am dumpheap your.package.name /sdcard/dumpheap.hprof

The hard way (don't remember why I've added it originally):

adb shell am dumpheap $(ps | grep your.package.name | awk '{print $2}') /sdcard/dumpheap.hprof

If your device doesn't have awk try to use busybox awk.

After that pull the created file, convert it with hprof-conv and open it in Android Studio.

like image 20
0neel Avatar answered Oct 02 '22 20:10

0neel