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:
adb jdwp
adb forward tcp:8000 jdwp:1234
(substitute output of 1 for 1234)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
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.
It will also trigger a full GC.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With