Skip to content Skip to sidebar Skip to footer

Android Count Number Of Threads For An App/process

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

Solution 1:

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   152102755490365213620000     ffffffff 00000000 S com.whatsapp
    u0_a96   15214152105490365213620000     ffffffff 00000000 S GC
    u0_a96   15215152105490365213620000     ffffffff 00000000 S Signal Catcher
    u0_a96   15216152105490365213620000     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.

Solution 2:

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.

Post a Comment for "Android Count Number Of Threads For An App/process"