赞
踩
如果程序输出下方的log或者弹出程序无响应就表示遇到了ANR
020-07-11 23:18:19.131 1232-28781/? E/ActivityManager: ANR in com.test (com.test/.MainActivity) PID: 28502 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: 14. Wait queue head age: 5581.9ms.) Load: 0.8 / 0.61 / 0.36 CPU usage from 88571ms to 372ms ago (2020-07-11 23:16:46.480 to 2020-07-11 23:18:14.680) with 99% awake: 15% 1232/system_server: 9.8% user + 5.2% kernel / faults: 48633 minor 2 major 3.9% 797/android.hardware.sensors@1.0-service-mediatek: 1.8% user + 2% kernel 2.5% 618/surfaceflinger: 1.5% user + 1% kernel / faults: 2480 minor 1.9% 25050/com.tencent.mm: 1.7% user + 0.2% kernel / faults: 11390 minor 4 major 0.9% 25228/com.android.packageinstaller: 0.6% user + 0.2% kernel / faults: 10014 minor 1.4% 24501/adbd: 0.2% user + 1.1% kernel / faults: 291099 minor 1.1% 10265/com.android.systemui: 0.8% user + 0.3% kernel / faults: 1011 minor 21 major 1% 2980/com.google.android.gms: 0.8% user + 0.2% kernel / faults: 5882 minor 19 major 1% 417/logd: 0.3% user + 0.6% kernel / faults: 32 minor 0.9% 155/chre_kthread: 0% user + 0.9% kernel 0.9% 9684/com.nearme.statistics.rom: 0.6% user + 0.2% kernel / faults: 2800 minor 1 major 0.8% 573/android.hardware.graphics.composer@2.1-service: 0.3% user + 0.4% kernel / faults: 1354 minor 0.4% 780/installd: 0% user + 0.3% kernel / faults: 751 minor 0.6% 2588/com.coloros.persist.system: 0.3% user + 0.2% kernel / faults: 1408 minor 0.5% 23765/kworker/0:3: 0% user + 0.5% kernel 0.4% 10919/com.xunmeng.pinduoduo:titan: 0.3% user + 0.1% kernel / faults: 1296 minor 1 major 0.4% 312/usbtemp_kthread: 0% user + 0.4% kernel 0.4% 2022/com.android.phone: 0.2% user + 0.1% kernel / faults: 960 minor 0.3% 9578/com.tencent.mm:push: 0.3% user + 0% kernel / faults: 999 minor 0.3% 28102/kworker/1:1: 0% user + 0.3% kernel 0.3% 2436/com.google.android.gms.persistent: 0.2% user + 0% kernel / faults: 1384 minor 24 major 0.2% 25284/com.oppo.market: 0.2% user + 0% kernel / faults: 1522 minor 0.2% 2056/com.oppo.launcher: 0.1% user + 0% kernel / faults: 1215 minor 1 major 0.2% 7/rcu_preempt: 0% user + 0.2% kernel 0.2% 287/ipi_cpu_dvfs_rt: 0% user + 0.2% kernel 0.1% 1977/com.coloros.safecenter:clear_filter: 0% user + 0% kernel / faults: 619 minor 4 major 0.2% 19229/kworker/u16:8: 0% user + 0.2% kernel 0.2% 9560/hif_thread: 0% user + 0.2% kernel 0.2% 9776/com.eg.android.AlipayGphone: 0.1% user + 0% kernel / faults: 66 minor 0.1% 18658/kworker/u16:0: 0% user + 0.1% kernel 0.1% 18843/kworker/u16:1: 0% user + 0.1% kernel 0.1% 418/servicemanager: 0% user + 0.1% kernel / faults: 1 minor 0.1% 548/zygote64: 0% user + 0.1% kernel / faults: 1485 minor 0.1% 771/aal: 0% user + 0.1% kernel 0.1% 796/media.codec: 0% user + 0% kernel / faults: 2022 minor 0.1% 26807/com.android.vending: 0.1% user + 0% kernel / faults: 330 minor 0.1% 26093/com.nearme.gamecenter: 0.1% user + 0% kernel / faults: 405 minor 1 major 0.1% 150/pbm: 0% user + 0.1% kernel 0.1% 572/android.hardware.graphics.allocator@2.0-service: 0% user + 0.1% kernel / faults: 168 minor 0.1% 574/android.hardware.health@2.0-service: 0% user + 0.1% kernel 0.1% 1086/com.tencent.mm:tools: 0% user + 0% kernel / faults: 72 minor 0.1% 19217/kworker/u17:2: 0% user + 0.1% kernel 0.1% 27111/com.coloros.digitalwellbeing: 0% user + 0% kernel / faults: 649 minor 0.1% 1//init: 0% user + 0% kernel / faults: 38 minor 0.1% 1185/disp_queue_P0: 0% user + 0.1% kernel 0.1% 9559/tx_thread: 0% user + 0.1% kernel 0.1% 26077/com.oppo.quicksearchbox: 0.1% user + 0% kernel / faults: 244 minor 0.1% 23535/kworker/u17:0: 0% user + 0.1% kernel 0.1% 181/disp_idlemgr: 0% user + 0.1% kernel 0.1% 19228/kworker/u16:7: 0% user + 0.1% kernel 0% 86/ion_mm_heap: 0% user + 0% kernel 0% 591/vendor.mediatek.hardware.power@2.1-service: 0% user + 0% kernel 0% 1285/mtkfusionrild: 0% user + 0% kernel 0% 17704/logcat: 0% user + 0% kernel 0% 22594/kworker/4:0: 0% user
日志并没有告诉我们是哪一行代码导致的耗时卡顿,那么如何查找导致ANR的元凶呢?
控制台输入下方代码
adb shell ls /data/anr/
将log日志从手机导入到桌面,
1 记得改成自己的文件地址
2文件导出过程较缓慢请耐心等待
3 一定要连接手机,如果失败就多试几次
adb bugreport /Users/liangchaojie/Desktop/
随意打开其中的一个anr日志,比如最下面的anr_2020-07-11-11-23-18-15-599
----- pid 28502 at 2020-07-11 23:18:15 ----- Cmd line: com.test Build fingerprint: 'OPPO/PCPM00/OP4ACF:9/PPR1.180610.011/1590563734:user/release-keys' ABI: 'arm64' Build type: optimized Zygote loaded classes=10781 post zygote classes=556 Intern table: 92927 strong; 364 weak JNI: CheckJNI is on; globals=600 (plus 32 weak) //中间的省略... //主要看这里 main函数里面 "main" prio=5 tid=1 Sleeping | group="main" sCount=1 dsCount=0 flags=1 obj=0x7a0982c0 self=0x7ab0014c00 | sysTid=28502 nice=-10 cgrp=default sched=0/0 handle=0x7b36af3548 | state=S schedstat=( 968443073 36962539 567 ) utm=86 stm=10 core=4 HZ=100 | stack=0x7fe2400000-0x7fe2402000 stackSize=8MB | held mutexes= at java.lang.Thread.sleep(Native method) - sleeping on <0x02d58282> (a java.lang.Object) at java.lang.Thread.sleep(Thread.java:373) - locked <0x02d58282> (a java.lang.Object) at java.lang.Thread.sleep(Thread.java:314) at com.test.MainActivity$1.onClick(MainActivity.java:22) at android.view.View.performClick(View.java:6724) at android.view.View.performClickInternal(View.java:6682) at android.view.View.access$3400(View.java:797) at android.view.View$PerformClick.run(View.java:26479) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:239) at android.app.ActivityThread.main(ActivityThread.java:7227) at java.lang.reflect.Method.invoke(Native method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:499) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:962) //剩下的省略...
找到了耗时ANR的原因,MainAcyivity里面的onClick方法使用了Thread.sleep导致的耗时,我们验证一下结果看下代码:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。