The sun JVM supports a -XX:+HeapDumpOnOutOfMemoryError
option to dump heap whenever a java process runs out of heap.
Is there a similar option on Android that will make an android app dump heap on an OutOfMemoryException? It can be difficult to try to time it properly when using DDMS manually.
Here is an improved version. On top of the original implementation this implementation also supports:
Usage: Call the static
initialize
method in your Application class in the onCreate method.To expand upon CommonsWare's answer:
I followed his suggestion successfully in my own Android app with the following code:
After the dump is created, you need to copy it from your phone to your PC: Click "Turn on USB storage" on the phone, find the file and copy it to your hard drive.
Then, if you want to use the Eclipse Memory Analyzer (MAT) to analyze the file, you will need to covert the file:
hprof-conv.exe dump.hprof dump-conv.hprof
(hprof-conv is located underandroid-sdk/tools
)Finally, open the
dump-conv.hprof
file with MATI have no idea if this works, but you might try adding a top-level exception handler, and in there asking for a heap dump if it is an
OutOfMemoryError
.