参考:
http://rayleeya.iteye.com/blog/1955652
https://codezjx.github.io/2017/08/06/anr-trace-analytics/
如何导出 /data/anr/traces.txt文件
先在c盘中建立好1.txt文件;
执行导出anr日志命令:
adb pull /data/anr/traces.txt C:/1.txt
记录anr日志,可通过以下开源框架
https://github.com/SalomonBrys/ANR-WatchDog
说明:
关于ANR traces的保存时长:
traces.txt:只保留最近一次发生ANR时的信息,位置:/data/anr/traces.txt
DropBox:会保留历史上发生的所有ANR的logs,位置:/data/system/dropbox,保存时长3天;
1. 卡住主线程分析例子
1. 1创建一个主线程的耗时操作
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 -----之间的为进程0000的所有线程堆栈信息;
- "main" prio=5 tid=1 Sleeping 分为为线程名、线程优先级(默认值5)、线程ID、线程状态;主线程之后会接着打印进程中其他线程的信息;
// 开头显示进程号、ANR发生的时间点和进程名称
----- 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
.....
还有很多信息
.....
直接定位到这里
//依次是:线程名、线程优先级、线程创建时的序号、线程当前状态
"main" prio=5 tid=1 Sleeping
//依次是:线程组名称、suspendCount、debugSuspendCount、线程的Java对象地址、线程的Native对象地址
| group="main" sCount=1 dsCount=0 obj=0x785d3dd0 self=0x7d2d8a1a00
//sysTid是线程号,主线程的线程号和进程号相同
| 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. 另外一个例子 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