Android计算一个应用程序/进程的线程数 [英] Android count number of threads for an app/process

查看:1228
本文介绍了Android计算一个应用程序/进程的线程数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要监视我的应用程序的行为并收集有关如何创建/销毁线程的统计信息.我知道DDMS有一个线程视图,可以实时显示此信息,但是我可以通过命令行获取相同的信息吗?我想创建自己的工具,该工具将记录此信息并对其进行处理.

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.

说明: 我正在寻找的是我可以通过ADB传递的命令,该命令将列出在进程下运行的线程.这样,我可以在不同的时间点运行命令,以获取一段时间内所有线程的状态(线程数及其名称).

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.

推荐答案

您可以通过两种方式进行此操作.
在Ubuntu 12.10上的Motorola Moto G上进行了尝试

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

  1. 您可以使用top(在ADB Shell下)列出 all 在设备上运行的线程.

  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

因此,要获取任何特定过程的详细信息,可以使用grep

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

如果不支持grep,请使用 Busybox .

如果要查找静态视图.您也可以使用ps.

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

其中15210是您的流程com.whatsapp

希望这可以解决您的问题,让我知道它是否有效.

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

这篇关于Android计算一个应用程序/进程的线程数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆