Android知识

打开 APP 启动几个 Binder 线程

2017-02-21  本文已影响1326人  FrankFan

之前有同事在看Framework的东西,启动APP时会启动 binder 线程,用于和 service 通信,但是没讨论出来怎么简单的验证一下。看 《Efficient Android Thread》看到 ps -t 可以查看线程,忽然想到这可以拿来用来看 binder 线程。以前只是用ps 来看哪些服务启动了,杀掉重启用,这回又多了一个用处。

打开 APP 前用下面的命令查看有多少 binder 线程,

➜  adb shell ps -t|grep binder|wc -l 
110

打开 APP 后

➜  adb shell ps -t|grep binder|wc -l 
112

用 对比进程

➜  diff 1.txt 2.txt
73,77c73,75
< u0_a14    2060  2039  1430112 71616 binder_thr 00000000 S Binder:2039_1
< u0_a14    2061  2039  1430112 71616 binder_thr 00000000 S Binder:2039_2
< u0_a14    2158  2039  1430112 71616 binder_thr 00000000 S Binder:2039_3
< u0_a76    2121  2106  1405096 50776 binder_thr 00000000 S Binder:2106_1
< u0_a76    2122  2106  1405096 50776 binder_thr 00000000 S Binder:2106_2
---
> u0_a14    2060  2039  1430152 72112 binder_thr 00000000 S Binder:2039_1
> u0_a14    2061  2039  1430152 72112 binder_thr 00000000 S Binder:2039_2
> u0_a14    2158  2039  1430152 72112 binder_thr 00000000 S Binder:2039_3

可以看出多了

< u0_a76    2121  2106  1405096 50776 binder_thr 00000000 S Binder:2106_1
< u0_a76    2122  2106  1405096 50776 binder_thr 00000000 S Binder:2106_2

还可以看到每一个 APP 启动了多少线程,
微信启动了 100多个线程,HelloWord级别的 APP 都会启动十几个线程

➜   adb shell ps -t|grep xxx
u0_a76    30905 1255  1405096 51056 SyS_epoll_ 00000000 S com.xxx.xxx
➜  practice adb shell ps -t|grep u0_a76
u0_a76    30905 1255  1405096 51056 SyS_epoll_ 00000000 S com.xxx.xxx
u0_a76    30910 30905 1405096 51056 futex_wait 00000000 S Jit thread pool
u0_a76    30911 30905 1405096 51056 do_sigtime 00000000 S Signal Catcher
u0_a76    30912 30905 1405096 51056 poll_sched 00000000 S JDWP
u0_a76    30913 30905 1405096 51056 futex_wait 00000000 S ReferenceQueueD
u0_a76    30914 30905 1405096 51056 futex_wait 00000000 S FinalizerDaemon
u0_a76    30915 30905 1405096 51056 futex_wait 00000000 S FinalizerWatchd
u0_a76    30916 30905 1405096 51056 futex_wait 00000000 S HeapTaskDaemon
u0_a76    30917 30905 1405096 51056 binder_thr 00000000 S Binder:30905_1
u0_a76    30918 30905 1405096 51056 binder_thr 00000000 S Binder:30905_2
u0_a76    30919 30905 1405096 51056 futex_wait 00000000 S Profile Saver
u0_a76    30920 30905 1405096 51056 __skb_recv 00000000 S Thread-3
u0_a76    30921 30905 1405096 51056 SyS_epoll_ 00000000 S RenderThread
u0_a76    30922 30905 1405096 51056 futex_wait 00000000 S hwuiTask1

Android 系统如果是 5.0 的 还可以看到 启动了 RendThread,这个佐证了 以前看到的5.0 做了很多性能方面的优化,把渲染的功从 UI 抽取出来放到新线程里 RendThread,使 动画更流畅。

上一篇 下一篇

猜你喜欢

热点阅读