5

I need to monitor the behaviour of my app and collect statistics about how threads are created/destroyed. I know DDMS has a thread view which shows this information live, but could I get the same information through the command line? I want to create my own tool which will log this information and the process it.

Clarification: What I am looking for is a command that I can pass via ADB, which will list the threads running under a process. This way, I can run the command at different points of time to get the status of all threads (number of threads & their names) over a period of time.

2
  • please be more specific about what you are trying to achieve. do you have some code to show? what have you tried?
    – timgeb
    Jun 9, 2014 at 17:39
  • 2
    I tried cat /proc/<PID>/status, and this gives me just the number of threads. I s there a better way through which I can get more details of the threads?
    – Sagar_R
    Jun 9, 2014 at 17:43

2 Answers 2

8

There are a two ways you can do this.
Tried this on a Motorola Moto G on Ubuntu 12.10

  1. You can list all the thread running on the device by using top (under ADB Shell).

    $ top -t
    PID   TID PR CPU% S     VSS     RSS PCY UID      Thread          Proc
    271   895  1   0% S  11120K   1892K     root     netd            /system/bin/netd
    272   272  0   0% S   1040K    200K     root     debuggerd       /system/bin/debuggerd
    274   274  2   0% S  63256K   7008K  fg system   surfaceflinger  /system/bin/surfaceflinger
    274   451  0   0% S  63256K   7008K  fg system   Binder_1        /system/bin/surfaceflinger
    

    So to get details for any particular process you can use grep

    $ top -t | grep com.whatsapp
    PID   TID   PR CPU% S     VSS     RSS PCY UID      Thread          Proc
    15210 15210  0   0% S 550076K  51180K  bg u0_a96   com.whatsapp    com.whatsapp
    15210 15214  0   0% S 550076K  51180K  bg u0_a96   GC              com.whatsapp
    15210 15215  0   0% S 550076K  51180K  bg u0_a96   Signal Catcher  com.whatsapp
    15210 15216  0   0% S 550076K  51180K  bg u0_a96   Compiler        com.whatsapp
    

    To run this on your host Machine just use

    $ adb shell top -t | grep com.whatsapp
    

    If grep is not supported, use Busybox.

  2. If you are looking for Static view. You can also use ps.

    $ ps -p 15210 -t                                         
    USER     PID   PPID  VSIZE  RSS   PRIO  NICE  RTPRI SCHED   WCHAN    PC         NAME
    u0_a96   15210 275   549036 52136 20    0     0     0     ffffffff 00000000 S com.whatsapp
    u0_a96   15214 15210 549036 52136 20    0     0     0     ffffffff 00000000 S GC
    u0_a96   15215 15210 549036 52136 20    0     0     0     ffffffff 00000000 S Signal Catcher
    u0_a96   15216 15210 549036 52136 20    0     0     0     ffffffff 00000000 S Compiler
    

    Where 15210 is the PID for your process com.whatsapp

Hope this solves your issues, let me know if it works.

0
7

In my limited knowledge, and with the chances that I may be totally wrong, have a look at:

public static Map<Thread,StackTraceElement[]> getAllStackTraces()    

Docs: http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#getAllStackTraces()

Returns a map of stack traces for all live threads. The map keys are threads and each map value is an array of StackTraceElement that represents the stack dump of the corresponding Thread. The returned stack traces are in the format specified for the getStackTrace method. The threads may be executing while this method is called. The stack trace of each thread only represents a snapshot and each stack trace may be obtained at different time. A zero-length array will be returned in the map value if the virtual machine has no stack trace information about a thread.

I hope that helped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.