How to make Java Thread Dump in Android?

Divano picture Divano · Nov 27, 2012 · Viewed 16.1k times · Source

I just want to get all dumps from java virtual machine threads, to look at what the threads lock and what threads waiting to unlock some resources. Something like is described here. I've tryed to kill Zygote process but with no results.

Answer

fadden picture fadden · Nov 27, 2012

The easiest way is with DDMS, or the ADT plugin in Eclipse. See http://developer.android.com/tools/debugging/ddms.html for basic instructions. In short, go into the Device view, select the application you're interested in, make sure thread updates are enabled, and switch to the Threads view. You will get a live-updated list of threads in that process. Double-clicking on a thread will grab a snapshot of the current stack state.

You can use select-all and copy in the thread dump to copy & paste the stack trace.

If you have a developer / rooted device, you can ask the Dalvik VM to dump thread stacks by sending a SIGQUIT to the app process you're interested in. For example, if you wanted to see the stacks for all threads in the Calendar app, you could do something like this:

% adb shell ps | grep android.calendar
u0_a6     2596  127   912804 48296 ffffffff b6f62c10 S com.google.android.calendar
# 2596 is the process ID
% adb shell run-as com.google.android.colendar kill -3 2596

The logcat output will say something like:

I/dalvikvm( 2596): Wrote stack traces to '/data/anr/traces.txt'

So, pull that:

% adb pull /data/anr/traces.txt .

Every time you signal a process, the logs are appended to that file. There may be other stuff in there, so you need to search for pid 2596:

----- pid 2596 at 2012-11-27 12:48:38 -----
Cmd line: com.google.android.calendar

DALVIK THREADS:
...

The advantage of doing this over the DDMS thread view is that, if the thread is stuck on a monitor, the stack dump will give you an indication of what object is locked and which thread currently holds the lock.

The zygote process isn't relevant here; by definition it isn't running an app. Since it doesn't have a JDWP thread, and doesn't listen for SIGQUIT, you can't get a stack trace out of it anyway.