Android技术知识Android开发经验谈Android开发

【Android】如何快速定位ANR

2019-12-09  本文已影响0人  风暴小狼

ANR(Android not responding)是指安卓程序无响应,android系统对于一些事件如果没有及时处理,在指定时间内没有完成则会造成anr。

哪些场景会出现anr

1.BroadcastQueue  :BroadcastReceiver在10s<前台> / 60s<后台>内无法处理完成
2.Service         :Service在20s<前台> / 10s<后台>内无法处理完成
3.InputDispatcher :按键或触摸事件在5s内无响应
4.ContentProvider :Provier在10s无法完成

如何分析anr

<1> 定位anr的具体位置
log中搜索关键字 "ANR in" ,快速定位出现anr的应用,查看 "Reason"定位anr原因,分析 "CPU usage"信息,查看是否哪个进程占用cpu过高

11-26 16:36:15.831 E/ActivityManager( 2763): ANR in com.xxx.launcher3 (com.xxx.launcher3/.LauncherActivity)
11-26 16:36:15.831 E/ActivityManager( 2763): PID: 4116
11-26 16:36:15.831 E/ActivityManager( 2763): Reason: Input dispatching timed out (Waiting to send key event because the focused window has not finished processing all of the input events that were previously delivered to it.  Outbound queue length: 0.  Wait queue length: 1.)
11-26 16:36:15.831 E/ActivityManager( 2763): Load: 8.25 / 2.17 / 0.73
11-26 16:36:15.831 E/ActivityManager( 2763): CPU usage from 17920ms to 0ms ago (2018-01-01 00:00:14.692 to 2019-11-26 16:36:13.933):
11-26 16:36:15.831 E/ActivityManager( 2763):   25% 2763/system_server: 18% user + 6.7% kernel / faults: 6833 minor 22 major
11-26 16:36:15.831 E/ActivityManager( 2763):   5% 1592/media.codec: 4.1% user + 0.9% kernel / faults: 10644 minor 36 major
11-26 16:36:15.831 E/ActivityManager( 2763):   4.7% 1556/surfaceflinger: 2.7% user + 1.9% kernel / faults: 392 minor
11-26 16:36:15.831 E/ActivityManager( 2763):   2.3% 1570/zygote: 0.6% user + 1.7% kernel / faults: 9274 minor
11-26 16:36:15.831 E/ActivityManager( 2763):   3.4% 901/mmcqd/0: 0% user + 3.4% kernel
11-26 16:36:15.831 E/ActivityManager( 2763):   3.2% 1007/HI_VPSS_Process: 0% user + 3.2% kernel
11-26 16:36:15.831 E/ActivityManager( 2763):   3.2% 1572/dtvserver: 1.1% user + 2% kernel / faults: 13 minor
11-26 16:36:15.831 E/ActivityManager( 2763):   1.8% 1561/tvserver: 0.8% user + 0.9% kernel / faults: 6 minor
11-26 16:36:15.831 E/ActivityManager( 2763):   1.3% 1552/logd: 0.5% user + 0.7% kernel / faults: 272 minor
11-26 16:36:15.831 E/ActivityManager( 2763):   0.8% 1553/lmkd: 0.3% user + 0.5% kernel
11-26 16:36:15.831 E/ActivityManager( 2763):   0.9% 1565/sharpservice: 0.5% user + 0.4% kernel / faults: 275 minor
11-26 16:36:15.831 E/ActivityManager( 2763):   0.5% 1941/main_thread: 0% user + 0.5% kernel
11-26 16:36:15.831 E/ActivityManager( 2763):   0.4% 1573/netd: 0% user + 0.4% kernel / faults: 1406 minor

<2> 分析trace文件
系统在出现anr时,会向文件data/trace/trace.txt写入关键信息,如上述步骤一种app:com.xxx.launcher3所示,trace.txt中会记录该app的相关信息:
查询关键字“com.xxx.launcher3” ,均是关于GC的相关信息,并且tid<线程>总计43个,其中13个在waiting,内存吃不消不断的gc,占用率都在99%,free memery只有5m,经过这样分析,结合app开发,发现app的图片框架和网络框架的线程池初始化是根据4核配置,但是平台是双核,需要进行缩减,优化后出现anr的概率大大下降。

----- pid 4116 at 2019-11-26 16:37:00 -----
Cmd line: com.xxx.launcher3
Build fingerprint: 'XXXX/lcd_xxeos5a/lcd_xxeos5a:7.0/NRD90M/1.02190906:user/release-keys'
ABI: 'arm'
Build type: optimized
Zygote loaded classes=4368 post zygote classes=1240
Intern table: 43259 strong; 314 weak
JNI: CheckJNI is off; globals=536 (plus 431 weak)
Libraries: /system/lib/libandroid.so /system/lib/libcompiler_rt.so /system/lib/libjavacrypto.so /system/lib/libjnigraphics.so /system/lib/libmedia_jni.so /system/lib/libmesalink-jni.so /system/lib/libwebviewchromium_loader.so libjavacore.so libopenjdk.so (9)
Heap: 26% free, 14MB/20MB; 70404 objects
Dumping cumulative Gc timings
Start Dumping histograms for 4 iterations for partial concurrent mark sweep
ProcessMarkStack:   Sum: 51.761ms 99% C.I. 0.002ms-17.868ms Avg: 4.313ms Max: 17.924ms
UpdateAndMarkImageModUnionTable:    Sum: 28.834ms 99% C.I. 0.896us-6290us Avg: 554.500us Max: 7155us
UpdateAndMarkZygoteModUnionTable:   Sum: 16.878ms 99% C.I. 1.588ms-6.878ms Avg: 4.219ms Max: 6.878ms
...
...
...
ScanGrayImageSpaceObjects:  Sum: 797us 99% C.I. 0.254us-457us Avg: 15.326us Max: 555us
MarkAllocStackAsLive:   Sum: 750us 99% C.I. 60us-330us Avg: 187.500us Max: 330us
SweepSystemWeaks:   Sum: 688us 99% C.I. 132us-196us Avg: 172us Max: 196us
ImageModUnionClearCards:    Sum: 513us 99% C.I. 0.252us-73.999us Avg: 4.932us Max: 91us
...
...
...
(Paused)ScanGrayZygoteSpaceObjects: Sum: 46us 99% C.I. 11us-13us Avg: 11.500us Max: 13us
SwapBitmaps:    Sum: 37us 99% C.I. 7us-11us Avg: 9.250us Max: 11us
ForwardSoftReferences:  Sum: 31us 99% C.I. 3us-20us Avg: 7.750us Max: 20us
...
...
...
Done Dumping histograms
sticky concurrent mark sweep paused:    Sum: 6.790ms 99% C.I. 1.076ms-2.261ms Avg: 1.697ms Max: 2.261ms
sticky concurrent mark sweep total time: 754.673ms mean time: 188.668ms
sticky concurrent mark sweep freed: 59119 objects with total size 5MB
sticky concurrent mark sweep throughput: 78407.2/s / 7MB/s
Total time spent in GC: 911.002ms
Mean GC size throughput: 5MB/s
Mean GC object throughput: 104879 objects/s
Total number of allocations 165949
Total bytes allocated 20MB
Total bytes freed 5MB
Free memory 5MB
Free memory until GC 5MB
Free memory until OOME 241MB
Total memory 20MB
Max memory 256MB

suspend all histogram:  Sum: 1.391ms 99% C.I. 6us-760.959us Avg: 86.937us Max: 814us
DALVIK THREADS (41):
"Signal Catcher" daemon prio=5 tid=3 Runnable
...
...
...
"Jit thread pool worker thread 0" prio=5 tid=2 Native (still starting up)
...
...
...
"Okio Watchdog" daemon prio=5 tid=43 TimedWaiting
----- end 4116 -----

如何避免ANR
1.合理使用 UI 主线程,耗时操作放入其他线程工作;
2.合理使用 Handler 异步消息处理机制来处理其他线程请求。
3.合理使用并遵循 Android 生命周期, 避免在 onCreate() and onResume()做过多的事情;
4.使用一些架构形成规范来避免内存等问题,例如:MVP、RxJava;
5.经常使用工具来检查内存问题,例如:MAT、TraceView、AS 自带等工具;
6.避免加载大图片引起内存不足导致 ANR;
7.避免内存泄露;

上一篇下一篇

猜你喜欢

热点阅读