前言:前段时间项目开发中遇到anr的问题,时间紧急,一时间又难以定位,通过临时方法解决后,最近有时间对ANR的问题做一次份细的解决方案,本文中的解决方案是通过综合其他博客后自己再通过实例验证后得出的可行方案,读者如遇类似问题可做参考,欢迎评论交流。
此次写了一个demo模拟长时间IO造成ANR的情况,然后对logcat内容和traces.txt分析 最终定位到问题代码的行号。
首先贴一下演示高IO的核心代码:
this.gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> gridView, View view,
int position, long id) {
Intent intent = null;
switch (position) {
case FILE_CHOOSER: {
int i =0;
String path = Environment.getExternalStorageDirectory().getPath();
while(i<1000000){
i++;
File dir = new File(path+"/ss");
if(!dir.exists()){
dir.mkdirs();
}
method2(path+"/ss"+"/anrtext.txt","ssssssssssssssssssssssssssddddddddddddddddddddddddddddddddddddddddsssssss");
}
}
//break;
case FULL_SCREEN_VIDEO: {
intent = new Intent(MainActivity.this,
FullScreenActivity.class);
MainActivity.this.startActivity(intent);
}
break;
case TBS_WEB: {
intent = new Intent(MainActivity.this,
Main2Activity.class);
MainActivity.this.startActivity(intent);
}
break;
}
}
});
主界面:
操作:点击“文件选择器”按钮开始进行100000次的写文件,再点击其他按钮。由于写入时间超过5秒,造成另一个按钮的点击无响应,然后界面弹出ANR弹框提示,模拟完成。
出现问题首先查看log信息,这也是每个开发人员解决所有项目问题的第一个意识,log如下
09-13 04:27:55.077 1627-1641/? E/ActivityManager: ANR in com.example.test_webview_demo (com.example.test_webview_demo/.MainActivity)
PID: 6472
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: 1. Wait queue head age: 5723.8ms.)
Load: 0.17 / 0.06 / 0.06
CPU usage from 162233ms to 0ms ago:
0.1% 6472/com.example.test_webview_demo: 0% user + 0% kernel / faults: 14553 minor
1.3% 1627/system_server: 0.2% user + 1% kernel / faults: 2974 minor
0.8% 1259/surfaceflinger: 0% user + 0.8% kernel
0% 1706/sdcard: 0% user + 0% kernel / faults: 2 minor
0.1% 1264/adbd: 0% user + 0.1% kernel / faults: 3124 minor
0.1% 1726/com.android.systemui: 0% user + 0% kernel / faults: 115 minor
0.1% 1982/com.android.phone: 0% user + 0% kernel / faults: 5 minor
0% 1352/mediaserver: 0% user + 0% kernel / faults: 136 minor
0% 1990/com.android.launcher3: 0% user + 0% kernel / faults: 457 minor
0% 1968/com.google.android.gms.persistent: 0% user + 0% kernel / faults: 4095 minor
0% 2398/com.google.android.gms: 0% user + 0% kernel / faults: 2074 minor
0% 10810/kworker/0:1: 0% user + 0% kernel
0% 3/ksoftirqd/0: 0% user + 0% kernel
0% 12/ksoftirqd/1: 0% user + 0% kernel
0% 8/rcu_preempt: 0% user + 0% kernel
0% 1239/kworker/0:1H: 0% user + 0% kernel
0% 1257/lmkd: 0% user + 0% kernel
0% 1258/servicemanager: 0% user + 0% kernel
0% 1348/netd: 0% user + 0% kernel / faults: 42 minor
0% 1360/fingerprintd: 0% user + 0% kernel
0% 2040/android.process.acore: 0% user + 0% kernel / faults: 16 minor
0% 2074/com.google.android.googlequicksearchbox:search: 0% user + 0% kernel / faults: 639 minor
0% 2108/com.google.process.gapps: 0% user + 0% kernel / faults: 171 minor
0% 6895/kworker/1:2: 0% user + 0% kernel
+0% 11600/logcat: 0% user + 0% kernel
2.8% TOTAL: 0.8% user + 2% kernel + 0% iowait + 0% softirq
CPU usage from 1471ms to 1979ms later:
81% 6472/com.example.test_webview_demo: 29% user + 52% kernel / faults: 1247 minor
50% 6472/st_webview_demo: 19% user + 31% kernel
17% 6482/HeapTaskDaemon: 3.8% user + 13% kernel
15% 6480/FinalizerDaemon: 3.8% user + 11% kernel
23% 1706/sdcard: 5.9% user + 17% kernel
21% 1712/sdcard: 5.9% user + 15% kernel
3.9% 1627/system_server: 0% user + 3.9% kernel
1.8% 1259/surfaceflinger: 0% user + 1.8% kernel
56% TOTAL: 18% user + 38% kernel
log中主要看五行:
1. E/ActivityManager: ANR in com.example.test_webview_demo (com.example.test_webview_demo/.MainActivity)
2. 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: 1. Wait queue head age: 5723.8ms.)
3. 2.8% TOTAL: 0.8% user + 2% kernel + 0% iowait + 0% softirq
4. 56% TOTAL: 18% user + 38% kernel
5. PID: 6472
log中搜索”ANR”可以找到这段log,五行分析如下:
从cpu使用情况看并不是cpu资源不足造成的卡死,所以应该是主线程在处理耗时操作造成阻塞引起的anr,从log中只能找出是mainActivity内发生,不够具体,如果mainActivity比较大的话也很难确定是在什么地方引起的,所以要进一步定位–分析traces.txt文件。
traces.txt系统自动生成的记录anr等异常的文件,只记录java代码产生的异常。
如果手机已经是完全root了的或者是模拟器,可以直接通过DDMS的File Explorer直接导出来:
如果不是root的手机,可以通过如下adb命令查看ANR日志文件位于哪里。
adb shell ls /data/anr/
然后通过adb的pull将日志文件拉取到指定的路径。
adb pull /data/anr/traces.txt d:/
但是如果手机没有进行root,执行adb pull命令就会出现如下提示:
remote object ‘/data/anr/traces.txt’ does not exist
这时候我们可以使用adb将文件copy一份到sdcard,然后再拉取出来:
adb shell
cat /data/anr/traces.txt >/mnt/sdcard/traces.txt
exit
然后可以再从sd卡考到项目目录:
D:\demos\X5WebDemo>adb pull /mnt/sdcard/traces.txt ./traces.txt
traces文件内容如下:
----- pid 6472 at 2018-09-13 04:27:53 -----
Cmd line: com.example.test_webview_demo
Build fingerprint: 'Android/sdk_google_phone_x86/generic_x86:6.0/MASTER/3738108:userdebug/test-keys'
ABI: 'x86'
Build type: optimized
Zygote loaded classes=3978 post zygote classes=52
Intern table: 44998 strong; 19 weak
JNI: CheckJNI is on; globals=300 (plus 58 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/libwebviewchromium_loader.so libjavacore.so (7)
Heap: 31% free, 3MB/4MB; 48092 objects
Dumping cumulative Gc timings
Start Dumping histograms for 17 iterations for partial concurrent mark sweep
ProcessMarkStack: Sum: 32.955ms 99% C.I. 8us-5074.500us Avg: 646.176us Max: 5094us
UpdateAndMarkImageModUnionTable: Sum: 30.824ms 99% C.I. 0.079ms-17.772ms Avg: 1.813ms Max: 19.022ms
SweepMallocSpace: Sum: 28.642ms 99% C.I. 10us-5383us Avg: 842.411us Max: 5390us
FinishPhase: Sum: 19.990ms 99% C.I. 0.022ms-3.723ms Avg: 1.175ms Max: 3.775ms
MarkRootsCheckpoint: Sum: 16.613ms 99% C.I. 44us-5915.999us Avg: 488.617us Max: 6764us
MarkConcurrentRoots: Sum: 14.340ms 99% C.I. 9us-1557.500us Avg: 421.764us Max: 1597us
MarkAllocStackAsLive: Sum: 10.843ms 99% C.I. 21us-2070.250us Avg: 637.823us Max: 2096us
AllocSpaceClearCards: Sum: 5.309ms 99% C.I. 8us-1031.999us Avg: 78.073us Max: 1072us
EnqueueFinalizerReferences: Sum: 4.611ms 99% C.I. 26us-2081us Avg: 271.235us Max: 2178us
ScanGrayAllocSpaceObjects: Sum: 2.640ms 99% C.I. 8us-320us Avg: 77.647us Max: 320us
ProcessCards: Sum: 1.728ms 99% C.I. 41us-95us Avg: 50.823us Max: 95us
MarkNonThreadRoots: Sum: 1.717ms 99% C.I. 11us-690.500us Avg: 50.500us Max: 738us
ReMarkRoots: Sum: 1.693ms 99% C.I. 43us-294us Avg: 99.588us Max: 294us
PreCleanCards: Sum: 1.613ms 99% C.I. 82us-113us Avg: 94.882us Max: 113us
MarkingPhase: Sum: 1.548ms 99% C.I. 75us-159us Avg: 91.058us Max: 159us
(Paused)PausePhase: Sum: 1.519ms 99% C.I. 74us-133us Avg: 89.352us Max: 133us
UpdateAndMarkZygoteModUnionTable: Sum: 1.136ms 99% C.I. 24us-465.999us Avg: 66.823us Max: 475us
MarkRoots: Sum: 1.105ms 99% C.I. 33us-187us Avg: 65us Max: 187us
RecursiveMark: Sum: 1.088ms 99% C.I. 16us-516us Avg: 64us Max: 522us
Sweep: Sum: 978us 99% C.I. 50us-80us Avg: 57.529us Max: 80us
ImageModUnionClearCards: Sum: 968us 99% C.I. 14us-70us Avg: 28.470us Max: 70us
ReclaimPhase: Sum: 946us 99% C.I. 49us-65us Avg: 55.647us Max: 65us
SweepLargeObjects: Sum: 806us 99% C.I. 33us-181us Avg: 47.411us Max: 181us
(Paused)ScanGrayAllocSpaceObjects: Sum: 778us 99% C.I. 8us-162us Avg: 22.882us Max: 162us
ZygoteModUnionClearCards: Sum: 652us 99% C.I. 9us-215.999us Avg: 19.176us Max: 232us
SweepSystemWeaks: Sum: 439us 99% C.I. 12us-60us Avg: 25.823us Max: 60us
ScanGrayImageSpaceObjects: Sum: 391us 99% C.I. 12us-85us Avg: 23us Max: 85us
ProcessReferences: Sum: 368us 99% C.I. 17us-38us Avg: 21.647us Max: 38us
SwapStacks: Sum: 329us 99% C.I. 16us-49us Avg: 19.352us Max: 49us
(Paused)ScanGrayImageSpaceObjects: Sum: 326us 99% C.I. 12us-47us Avg: 19.176us Max: 47us
(Paused)ProcessMarkStack: Sum: 271us 99% C.I. 8us-100us Avg: 15.941us Max: 100us
(Paused)ScanGrayZygoteSpaceObjects: Sum: 238us 99% C.I. 8us-39us Avg: 14us Max: 39us
RevokeAllThreadLocalAllocationStacks: Sum: 223us 99% C.I. 10us-21us Avg: 13.117us Max: 21us
ScanGrayZygoteSpaceObjects: Sum: 204us 99% C.I. 9us-26us Avg: 12us Max: 26us
SwapBitmaps: Sum: 202us 99% C.I. 10us-17us Avg: 11.882us Max: 17us
BindBitmaps: Sum: 165us 99% C.I. 8us-21us Avg: 9.705us Max: 21us
InitializePhase: Sum: 160us 99% C.I. 8us-20us Avg: 9.411us Max: 20us
PreSweepingGcVerification: Sum: 146us 99% C.I. 8us-11us Avg: 8.588us Max: 11us
UnBindBitmaps: Sum: 140us 99% C.I. 8us-10us Avg: 8.235us Max: 10us
FindDefaultSpaceBitmap: Sum: 139us 99% C.I. 8us-9us Avg: 8.176us Max: 9us
SweepZygoteSpace: Sum: 138us 99% C.I. 8us-10us Avg: 8.117us Max: 10us
Done Dumping histograms
partial concurrent mark sweep paused: Sum: 7.348ms 99% C.I. 247us-774us Avg: 432.235us Max: 774us
partial concurrent mark sweep total time: 188.921ms mean time: 11.113ms
partial concurrent mark sweep freed: 324155 objects with total size 44MB
partial concurrent mark sweep throughput: 1.72423e+06/s / 235MB/s
Start Dumping histograms for 225 iterations for sticky concurrent mark sweep
FreeList: Sum: 314.322ms 99% C.I. 11us-1476us Avg: 322.712us Max: 2590us
FinishPhase: Sum: 175.609ms 99% C.I. 15us-2146.875us Avg: 780.484us Max: 3070us
MarkConcurrentRoots: Sum: 150.037ms 99% C.I. 8us-1118.750us Avg: 333.415us Max: 2877us
SweepArray: Sum: 136.460ms 99% C.I. 57us-2675us Avg: 606.488us Max: 6441us
MarkingPhase: Sum: 126.659ms 99% C.I. 143us-1675us Avg: 562.928us Max: 2189us
MarkRootsCheckpoint: Sum: 101.272ms 99% C.I. 42us-1437.500us Avg: 225.048us Max: 2555us
ScanGrayAllocSpaceObjects: Sum: 87.008ms 99% C.I. 8us-800us Avg: 96.675us Max: 1994us
ProcessReferences: Sum: 72.197ms 99% C.I. 24us-799.553us Avg: 320.875us Max: 65711us
ResetStack: Sum: 67.416ms 99% C.I. 11us-1097.916us Avg: 299.626us Max: 3523us
ProcessMarkStack: Sum: 32.287ms 99% C.I. 7us-240.624us Avg: 35.874us Max: 1403us
ProcessCards: Sum: 23.089ms 99% C.I. 40us-237.500us Avg: 51.308us Max: 424us
PreCleanCards: Sum: 23.012ms 99% C.I. 81us-443.750us Avg: 102.275us Max: 659us
(Paused)PausePhase: Sum: 21.670ms 99% C.I. 73us-393.750us Avg: 96.311us Max: 510us
AllocSpaceClearCards: Sum: 21.583ms 99% C.I. 8us-136.111us Avg: 23.981us Max: 207us
ReMarkRoots: Sum: 20.613ms 99% C.I. 41us-396.875us Avg: 91.613us Max: 654us
EnqueueFinalizerReferences: Sum: 16.414ms 99% C.I. 24us-247.916us Avg: 72.951us Max: 2327us
ReclaimPhase: Sum: 15.761ms 99% C.I. 50.030us-493.750us Avg: 70.048us Max: 660us
MarkRoots: Sum: 12.784ms 99% C.I. 32us-346.875us Avg: 56.817us Max: 1521us
ScanGrayImageSpaceObjects: Sum: 12.169ms 99% C.I. 12us-158.333us Avg: 27.042us Max: 1382us
ImageModUnionClearCards: Sum: 11.478ms 99% C.I. 14us-146.875us Avg: 25.506us Max: 225us
MarkNonThreadRoots: Sum: 8.494ms 99% C.I. 10us-137.500us Avg: 18.875us Max: 208us
(Paused)ScanGrayAllocSpaceObjects: Sum: 8.091ms 99% C.I. 8us-143.750us Avg: 17.980us Max: 209us
ScanGrayZygoteSpaceObjects: Sum: 6.616ms 99% C.I. 8us-93.750us Avg: 14.702us Max: 418us
ZygoteModUnionClearCards: Sum: 6.355ms 99% C.I. 8us-87.500us Avg: 14.122us Max: 357us
SweepSystemWeaks: Sum: 5.903ms 99% C.I. 13us-287.500us Avg: 26.235us Max: 602us
ForwardSoftReferences: Sum: 5.885ms 99% C.I. 16us-143.750us Avg: 26.155us Max: 1584us
SwapStacks: Sum: 4.276ms 99% C.I. 16us-93.750us Avg: 19.004us Max: 311us
(Paused)ScanGrayImageSpaceObjects: Sum: 3.961ms 99% C.I. 12us-56us Avg: 17.604us Max: 56us
SwapBitmaps: Sum: 3.705ms 99% C.I. 9us-237.500us Avg: 16.466us Max: 642us
RevokeAllThreadLocalAllocationStacks: Sum: 3.092ms 99% C.I. 9us-49.972us Avg: 13.742us Max: 264us
InitializePhase: Sum: 2.920ms 99% C.I. 8us-193.750us Avg: 12.977us Max: 301us
(Paused)ScanGrayZygoteSpaceObjects: Sum: 2.530ms 99% C.I. 8us-93.750us Avg: 11.244us Max: 105us
FindDefaultSpaceBitmap: Sum: 2.512ms 99% C.I. 8us-93.749us Avg: 11.164us Max: 124us
(Paused)ProcessMarkStack: Sum: 2.472ms 99% C.I. 8us-96.875us Avg: 10.986us Max: 184us
BindBitmaps: Sum: 2.425ms 99% C.I. 8us-49.972us Avg: 10.777us Max: 50us
UnBindBitmaps: Sum: 2.422ms 99% C.I. 8us-96.875us Avg: 10.764us Max: 134us
RecordFree: Sum: 2.200ms 99% C.I. 8us-93.750us Avg: 9.777us Max: 118us
PreSweepingGcVerification: Sum: 2.022ms 99% C.I. 7us-36us Avg: 8.986us Max: 36us
Done Dumping histograms
sticky concurrent mark sweep paused: Sum: 83.450ms 99% C.I. 249us-1225us Avg: 370.888us Max: 1225us
sticky concurrent mark sweep total time: 1.517s mean time: 6.745ms
sticky concurrent mark sweep freed: 879576 objects with total size 514MB
sticky concurrent mark sweep throughput: 579813/s / 339MB/s
Total time spent in GC: 1.706s
Mean GC size throughput: 327MB/s
Mean GC object throughput: 705318 objects/s
Total number of allocations 1251962
Total bytes allocated 562MB
Total bytes freed 558MB
Free memory 1487KB
Free memory until GC 1487KB
Free memory until OOME 316MB
Total memory 4MB
Max memory 320MB
Zygote space size 876KB
Total mutator paused time: 90.798ms
Total time waiting for GC to complete: 4.519ms
Total GC count: 242
Total GC time: 1.706s
Total blocking GC count: 0
Total blocking GC time: 0
Histogram of GC count per 10000 ms: 0:511,4:1
Histogram of blocking GC count per 10000 ms: 0:512
suspend all histogram: Sum: 10.323ms 99% C.I. 16us-1017.599us Avg: 40.482us Max: 1463us
DALVIK THREADS (12):
"main" prio=5 tid=1 Runnable
| group="main" sCount=0 dsCount=0 obj=0x742e7258 self=0xb4034500
| sysTid=6472 nice=0 cgrp=default sched=0/0 handle=0xb7706c00
| state=R schedstat=( 0 0 0 ) utm=172 stm=157 core=1 HZ=100
| stack=0xbf028000-0xbf02a000 stackSize=8MB
| held mutexes= "mutator lock"(shared held)
at java.lang.StringBuilder.(StringBuilder.java:54)
at com.example.test_webview_demo.MainActivity$1.onItemClick(MainActivity.java:115)
at android.widget.AdapterView.performItemClick(AdapterView.java:310)
at android.widget.AbsListView.performItemClick(AbsListView.java:1145)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3042)
at android.widget.AbsListView.onTouchUp(AbsListView.java:3891)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:3656)
at android.view.View.dispatchTouchEvent(View.java:9294)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2547)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2240)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2403)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1737)
at android.app.Activity.dispatchTouchEvent(Activity.java:2765)
at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2364)
at android.view.View.dispatchPointerEvent(View.java:9514)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4230)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4096)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3787)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3844)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5922)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5896)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5857)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6025)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native method)
at android.os.MessageQueue.next(MessageQueue.java:323)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke!(Native method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
"Signal Catcher" daemon prio=5 tid=2 Runnable
| group="system" sCount=0 dsCount=0 obj=0x12c760a0 self=0xae8f2000
| sysTid=6477 nice=0 cgrp=default sched=0/0 handle=0xb3415930
| state=R schedstat=( 0 0 0 ) utm=0 stm=0 core=0 HZ=100
| stack=0xb3319000-0xb331b000 stackSize=1014KB
| held mutexes= "mutator lock"(shared held)
native: #00 pc 0058bd02 /system/lib/libart.so (art::DumpNativeStack(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, int, char const*, art::ArtMethod*, void*)+226)
native: #01 pc 0055194e /system/lib/libart.so (art::Thread::Dump(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) const+286)
native: #02 pc 0055c25b /system/lib/libart.so (art::DumpCheckpoint::Run(art::Thread*)+923)
native: #03 pc 0055edfd /system/lib/libart.so (art::ThreadList::RunCheckpoint(art::Closure*)+301)
native: #04 pc 00561c8e /system/lib/libart.so (art::ThreadList::DumpForSigQuit(std::__1::basic_ostream<char, std::__1::char_traits<char> >&)+770)
native: #05 pc 005135c6 /system/lib/libart.so (art::Runtime::DumpForSigQuit(std::__1::basic_ostream<char, std::__1::char_traits<char> >&)+140)
native: #06 pc 005253bd /system/lib/libart.so (art::SignalCatcher::HandleSigQuit()+1901)
native: #07 pc 00526613 /system/lib/libart.so (art::SignalCatcher::Run(void*)+883)
native: #08 pc 00080aa3 /system/lib/libc.so (__pthread_start(void*)+56)
native: #09 pc 00021952 /system/lib/libc.so (__start_thread+25)
native: #10 pc 000170b6 /system/lib/libc.so (__bionic_clone+70)
(no managed stack frames)
分析如下:从前文log中知道发生anr的进程号为6472,所以首先定位到6472进程下的信息,下面有多个线程(此处因篇幅限制只贴出两个线程的信息),找tid为1的线程(tid为1即为主线程,因为此处anr是主线程阻塞造成,而不是其他线程或进程引起的资源不足)找到项目代码:at com.example.test_webview_demo.MainActivity$1.onItemClick(MainActivity.java:115),明确指出是MainActivity.java的第115行造成的阻塞引起的点击长时间无响应,至此定位完成。
在Android里,应用程序的响应是由ActivityManager和WindowManager服务系统服务监视的,当检测到下面三种情况的任何一种时,Android就会针对特定的应用程序显示ANR对话框。
造成ANR的原因有很多,无论是在Activity或者BroadcastReceiver还是在Service,我们看到都是在主线程中操作引起的ANR,因此我们应该避免在主线程做太多耗时的操作,网络请求不用说了,Android4.0以后就禁止在主线程成执行请求了,除此之外就是要注意如下几个方面: