案例1
ANR提示:
03-13 21:11:44.813 E/ANRManager( 820): ANR in com.my.fm
03-13 21:11:44.813 E/ANRManager( 820): Reason: Executing service com.my.fm/.common.downloadmgr.DownloadService
第一反应是查看DownloadService的生命周期函数onCreate和onStartCommand里面有没有耗时操作,检查完毕,并没有,懵逼。
然后我在ANR日志中观察到如下数据:
03-13 21:11:37.806 I/ANRManager( 820): getProcessState
03-13 21:11:37.808 I/ActivityManager( 820): Android time :[2017-03-13 21:11:37.805] [8809.438]
03-13 21:11:37.808 I/ActivityManager( 820): CPU usage from 7105ms to 27ms ago:
03-13 21:11:37.808 I/ActivityManager( 820): 98% 12850/com.my.fm: 98% user + 0.1% kernel / faults: 15 minor
03-13 21:11:37.808 I/ActivityManager( 820): 7% 3510/cn.testin.itestin:nserver: 5.9% user + 1.1% kernel / faults: 3367 minor
03-13 21:11:37.808 I/ActivityManager( 820): 5.7% 820/system_server: 3.2% user + 2.5% kernel / faults: 61 minor
03-13 21:11:37.808 I/ActivityManager( 820): 3.3% 246/surfaceflinger: 0.5% user + 2.8% kernel
03-13 21:11:37.808 I/ActivityManager( 820): 2.1% 21712/com.android.launcher3: 0.9% user + 1.1% kernel
03-13 21:11:37.808 I/ActivityManager( 820): 1.6% 297/adbd: 0.7% user + 0.9% kernel / faults: 120 minor
03-13 21:11:37.808 I/ActivityManager( 820): 1.2% 12711/com.lbe.security.meizu:service: 1.1% user + 0.1% kernel
03-13 21:11:37.808 I/ActivityManager( 820): 0.9% 3539/cn.testin.itestin:reader: 0.4% user + 0.5% kernel
03-13 21:11:37.808 I/ActivityManager( 820): 0.7% 1342/com.meizu.cloud: 0.4% user + 0.2% kernel
03-13 21:11:37.808 I/ActivityManager( 820): 0.7% 12335/logcat: 0.1% user + 0.5% kernel
03-13 21:11:37.808 I/ActivityManager( 820): 0.5% 6527/com.android.systemui: 0.2% user + 0.2% kernel / faults: 13 minor
03-13 21:11:37.808 I/ActivityManager( 820): 0.5% 19427/kworker/0:0: 0% user + 0.5% kernel
03-13 21:11:37.808 I/ActivityManager( 820): 0.4% 6874/kworker/u16:3: 0% user + 0.4% kernel
03-13 21:11:37.808 I/ActivityManager( 820): 0.2% 121/fence_worker: 0% user + 0.2% kernel
03-13 21:11:37.808 I/ActivityManager( 820): 0.2% 128/bat_thread_kthr: 0% user + 0.2% kernel
03-13 21:11:37.808 I/ActivityManager( 820): 0.2% 129/mtk charger_hv_: 0% user + 0.2% kernel
03-13 21:11:37.808 I/ActivityManager( 820): 0.2% 813/gsm0710muxd: 0% user + 0.2% kernel
03-13 21:11:37.808 I/ActivityManager( 820): 0.2% 12134/kworker/u16:0: 0% user + 0.2% kernel
03-13 21:11:37.808 I/ActivityManager( 820): 0.1% 8/rcu_preempt: 0% user + 0.1% kernel
03-13 21:11:37.808 I/ActivityManager( 820): 0.1% 57/cfinteractive: 0% user + 0.1% kernel
03-13 21:11:37.808 I/ActivityManager( 820): 0.1% 61/hps_main: 0% user + 0.1% kernel
03-13 21:11:37.808 I/ActivityManager( 820): 0.1% 225/healthd: 0% user + 0.1% kernel
03-13 21:11:37.808 I/ActivityManager( 820): 0.1% 241/netd: 0% user + 0.1% kernel / faults: 12 minor
03-13 21:11:37.808 I/ActivityManager( 820): 0.1% 11546/uiautomator: 0.1% user + 0% kernel / faults: 3 minor
03-13 21:11:37.808 I/ActivityManager( 820): 0.1% 30849/tx_thread: 0% user + 0.1% kernel
03-13 21:11:37.808 I/ActivityManager( 820): 0.1% 30850/hif_thread: 0% user + 0.1% kernel
03-13 21:11:37.808 I/ActivityManager( 820): 0% 30851/rx_thread: 0% user + 0% kernel
03-13 21:11:37.808 I/ActivityManager( 820): +0% 12945/app_process: 0% user + 0% kernel
03-13 21:11:37.808 I/ActivityManager( 820): +0% 12961/top: 0% user + 0% kernel
03-13 21:11:37.808 I/ActivityManager( 820): 0.1% TOTAL: 0.1% user + 0% kernel + 0% softirq
03-13 21:11:37.808 I/ActivityManager( 820):
发现我们的应用myapp占用CPU达到了98%
这时候就有另一个思路,即使DownloadService本身没有耗时操作,但是如果有另一个任务或者线程频繁占用CPU,这么最终会拖累DownloadService的执行,同样会引发DownloadService的ANR。
接着观察到下面的数据
"Thread-2250" prio=5 tid=13 SUSPENDED
| group="main" sCount=1 dsCount=0 obj=0x422a65c8 self=0x788a60a8
| sysTid=12902 nice=0 sched=0/0 cgrp=default handle=2022342448
| state=S schedstat=( 19713394162 221285073 1557 ) utm=1969 stm=2 core=2
at java.util.ArrayList.isEmpty(ArrayList.java:~323)
at java.util.Collections$SynchronizedCollection.isEmpty(Collections.java:410)
at com.my.fm.common.util.f.a(Tool.java:273)
at com.my.fm.common.downloadmgr.d$d.b(DownloadManager.java:1453)
at com.my.common.downloadmgr.d.run(DownloadManager.java:274)
好,有点可疑,我就去定位(DownloadManager.java:1453)这行代码,发现这行代码是在一个while死循环里运行
@Override
public void run() {
super.run();
while (isRunning) { //isRunning在某些时候一直为true.
//DownloadManager code
}
}
}
虽然while循环在非主线程运行,但是会一直请求CPU占用,所以这处代码就像地雷,如何改正呢?很简单,使用Thread.sleep即可:
@Override
public void run() {
super.run();
while (isRunning) {
//my working code
//防止一直while占用大量CPU时间,造成ANR
try {
Thread.sleep(800);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
结论:不仅仅是不要在主线程做耗时操作,并发线程过多,或者非主线程长时间占用CPU, 同样会造成UI卡顿。