Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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.

like image 248
Sagar_R Avatar asked Jun 09 '14 17:06

Sagar_R


People also ask

How can I tell how many threads a process is using?

To view the threads in a process with Process Explorer, select a process and open the process properties (double-click on the process or click on the Process, Properties menu item). Then click on the Threads tab. This tab shows a list of the threads in the process and three columns of information.

How many threads can an Android app have?

Seven Threading Patterns in Android.

How many threads can be executed at a time in Android?

Processor with 2 core will handle 2 threads at a time( concurrent execution of two threads). Processor with 4 core will handle 4 threads at a time( concurrent execution of four threads.


Video Answer


2 Answers

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.

like image 115
Saurabh Meshram Avatar answered Sep 27 '22 18:09

Saurabh Meshram


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.

like image 27
An SO User Avatar answered Sep 27 '22 17:09

An SO User