參考:
http://rayleeya.iteye.com/blog/1955652
https://codezjx.github.io/2017/08/06/anr-trace-analytics/
如何導(dǎo)出 /data/anr/traces.txt文件
先在c盤中建立好1.txt文件浓若;
執(zhí)行導(dǎo)出anr日志命令:
adb pull /data/anr/traces.txt C:/1.txt
記錄anr日志卓舵,可通過以下開源框架
https://github.com/SalomonBrys/ANR-WatchDog
說明:
關(guān)于ANR traces的保存時(shí)長:
traces.txt:只保留最近一次發(fā)生ANR時(shí)的信息同规,位置:/data/anr/traces.txt
DropBox:會保留歷史上發(fā)生的所有ANR的logs友题,位置:/data/system/dropbox拇泣,保存時(shí)長3天;
1. 卡住主線程分析例子
1. 1創(chuàng)建一個(gè)主線程的耗時(shí)操作
Toast.makeText(getApplicationContext(), "卡死UI", Toast.LENGTH_SHORT).show();
SystemClock.sleep(10 * 10000);
1.2 logcat 中的打印
基本上彩届,可以看到問題所在拾并,那就是 reason
PID: 25719
Reason: Input dispatching timed out (Waiting to send non-key event because the touched window has not finished processing certain input events that were delivered to it over 500.0ms ago. Wait queue length: 17. Wait queue head age: 8816.9ms.)
Load: 28.45 / 28.73 / 28.92
CPU usage from 103375ms to 0ms ago (2017-11-13 16:48:13.231 to 2017-11-13 16:49:56.606) with 99% awake:
5.5% 1264/system_server: 3.9% user + 1.5% kernel / faults: 1300 minor
5.1% 442/surfaceflinger: 2.5% user + 2.6% kernel / faults: 526 minor
2.9% 1519/com.android.systemui: 2.5% user + 0.4% kernel / faults: 1186 minor
2.2% 624/cameraserver: 0.9% user + 1.3% kernel
1.3 traces 文件說明
- 介于----- pid 0000 xxx -----與----- end 0000 -----之間的為進(jìn)程0000的所有線程堆棧信息混埠;
- "main" prio=5 tid=1 Sleeping 分為為線程名怠缸、線程優(yōu)先級(默認(rèn)值5)、線程ID钳宪、線程狀態(tài)揭北;主線程之后會接著打印進(jìn)程中其他線程的信息;
// 開頭顯示進(jìn)程號、ANR發(fā)生的時(shí)間點(diǎn)和進(jìn)程名稱
----- pid 25719 at 2017-11-13 16:49:56 -----
Cmd line: better.anr
Build fingerprint: 'HUAWEI/NXT-AL10/HWNXT:7.0/HUAWEINXT-AL10/C00B592:user/release-keys'
ABI: 'arm64'
Build type: optimized
Zygote loaded classes=4418 post zygote classes=508
Intern table: 77119 strong; 174 weak
JNI: CheckJNI is off; globals=502 (plus 124 weak)
Libraries: /system/lib64/libandroid.so /system/lib64/libcompiler_rt.so /system/lib64/libhwaps.so /system/lib64/libjavacrypto.so /system/lib64/libjnigraphics.so /system/lib64/libmedia_jni.so /system/lib64/libwebviewchromium_loader.so libjavacore.so libopenjdk.so (9)
Heap: 28% free, 4MB/6MB; 32412 objects
Dumping cumulative Gc timings
Start Dumping histograms for 1 iterations for partial concurrent mark sweep
.....
還有很多信息
.....
直接定位到這里
//依次是:線程名吏颖、線程優(yōu)先級搔体、線程創(chuàng)建時(shí)的序號、線程當(dāng)前狀態(tài)
"main" prio=5 tid=1 Sleeping
//依次是:線程組名稱半醉、suspendCount疚俱、debugSuspendCount、線程的Java對象地址缩多、線程的Native對象地址
| group="main" sCount=1 dsCount=0 obj=0x785d3dd0 self=0x7d2d8a1a00
//sysTid是線程號呆奕,主線程的線程號和進(jìn)程號相同
| sysTid=25719 nice=-10 cgrp=default sched=0/0 handle=0x7d319e3a98
| state=S schedstat=( 374569801 11467189 513 ) utm=31 stm=6 core=2 HZ=100
| stack=0x7ff8020000-0x7ff8022000 stackSize=8MB
| held mutexes=
at java.lang.Thread.sleep!(Native method)
-sleeping on <0x0a871b14> (a java.lang.Object)
at java.lang.Thread.sleep(Thread.java:379)
-locked <0x0a871b14> (a java.lang.Object)
at java.lang.Thread.sleep(Thread.java:321)
at android.os.SystemClock.sleep(SystemClock.java:120)
at better.anr.MainActivity.anrMainThread(MainActivity.java:33)
at better.anr.MainActivity.onClick(MainActivity.java:25)
at android.view.View.performClick(View.java:5646)
at android.view.View$PerformClick.run(View.java:22459)
at android.os.Handler.handleCallback(Handler.java:761)
at android.os.Handler.dispatchMessage(Handler.java:98)
.....
還有很多信息
.....
----- end 25719 -----
2. 另外一個(gè)例子 dispatching timeout
logcat 打印如下:
Reason: Input dispatching timed out (Waiting to send non-key event because the touched window has not finished processing certain input events that were delivered to it over 500.0ms