【Android】如何快速定位ANR

ANR(Android not responding)是指安卓程序無響應,android系統(tǒng)對于一些事件如果沒有及時處理,在指定時間內(nèi)沒有完成則會造成anr。

哪些場景會出現(xiàn)anr?

1.BroadcastQueue  :BroadcastReceiver在10s<前臺> / 60s<后臺>內(nèi)無法處理完成
2.Service         :Service在20s<前臺> / 10s<后臺>內(nèi)無法處理完成
3.InputDispatcher :按鍵或觸摸事件在5s內(nèi)無響應
4.ContentProvider :Provier在10s無法完成

如何分析anr ?

<1> 定位anr的具體位置
log中搜索關(guān)鍵字 "ANR in" ,快速定位出現(xiàn)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文件
系統(tǒng)在出現(xiàn)anr時,會向文件data/trace/trace.txt寫入關(guān)鍵信息,如上述步驟一種app:com.xxx.launcher3所示,trace.txt中會記錄該app的相關(guān)信息:
查詢關(guān)鍵字“com.xxx.launcher3” ,均是關(guān)于GC的相關(guān)信息,并且tid<線程>總計43個,其中13個在waiting,內(nèi)存吃不消不斷的gc,占用率都在99%,free memery只有5m,經(jīng)過這樣分析,結(jié)合app開發(fā),發(fā)現(xiàn)app的圖片框架和網(wǎng)絡框架的線程池初始化是根據(jù)4核配置,但是平臺是雙核,需要進行縮減,優(yōu)化后出現(xiàn)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.使用一些架構(gòu)形成規(guī)范來避免內(nèi)存等問題,例如:MVP、RxJava;
5.經(jīng)常使用工具來檢查內(nèi)存問題,例如:MAT、TraceView、AS 自帶等工具;
6.避免加載大圖片引起內(nèi)存不足導致 ANR;
7.避免內(nèi)存泄露;

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容