Android ANR问题分析总结

为什么会产生

一般是在“主”线程中进行一些长时间的操作时,会发生ANR。这是事件循环线程,如果繁忙,则Android无法处理应用程序中的任何其他GUI事件,因此会引发ANR
出现以下任何情况时,系统都会针对您的应用触发 ANR:

  1. 当您的 Activity 位于前台时,您的应用在 5 秒钟内未响应输入事件或BroadcastReceiver(如按键或屏幕轻触事件)
  2. 虽然前台没有 Activity,但您的 BroadcastReceiver 用了相当长的时间仍未执行完毕。

出现的场景

打开app,每次进入购物车模块,当网络状态不好的时候,再切换到其它模块就会出现ANR

诊断 ANR

诊断 ANR 时需要考虑以下几种常见模式:

  1. 应用在主线程上非常缓慢地执行涉及 I/O 的操作。
  2. 应用在主线程上进行长时间的计算。
  3. 主线程在对另一个进程进行同步 binder 调用,而后者需要很长时间才能返回。
  4. 主线程处于阻塞状态,为发生在另一个线程上的长操作等待同步的块。(本次ANR属于这种情况)
  5. 主线程在进程中或通过 binder 调用与另一个线程之间发生死锁。主线程不只是在等待长操作执行完毕,而且处于死锁状态。

分析步骤

  • 看Logcat打印
ANR in 包名 (包名/.MainActivity)
    PID: 15350
    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. waitqueue length = 8, head.seq = 3376085, Wait queue head age: 8115.9ms.)
    Parent: 包名/.MainActivity
    Load: 0.0 / 0.0 / 0.0
    CPU usage from 24627ms to 0ms ago (2020-01-15 16:59:46.273 to 2020-01-15 17:00:10.900):
      26% 1254/system_server: 18% user + 7.8% kernel / faults: 109111 minor 1 major
      1.1% 15350/usap64: 0.9% user + 0.1% kernel / faults: 141417 minor
      12% 2127/com.android.systemui: 9.4% user + 2.9% kernel / faults: 28273 minor 3 major
      9.1% 704/surfaceflinger: 4.3% user + 4.7% kernel / faults: 966 minor
      6.7% 788/aptouch_daemon: 5.9% user + 0.7% kernel / faults: 137 minor
      5.6% 662/[email protected]: 1.9% user + 3.6% kernel / faults: 101 minor
      5.2% 678/[email protected]: 3.2% user + 2% kernel / faults: 76 minor
      4.8% 13166/usap64: 3.6% user + 1.1% kernel / faults: 14193 minor
      4% 7836/adbd: 0.9% user + 3% kernel / faults: 280 minor
      3.3% 477/logd: 0.8% user + 2.5% kernel / faults: 95 minor
      3.1% 842/dubaid: 1.8% user + 1.2% kernel / faults: 1369 minor
      2.2% 6143/kworker/u16:9: 0% user + 2.2% kernel
      2.2% 2433/com.android.phone: 1.3% user + 0.8% kernel / faults: 2189 minor
      2% 661/[email protected]: 0% user + 2% kernel / faults: 169 minor
      2% 6343/kworker/u16:26: 0% user + 2% kernel
      2% 1866/pcie_rc_pm: 0% user + 2% kernel
      0.3% 15547/kworker/u16:2: 0% user + 0.3% kernel / faults: 1 minor
      1.8% 16445/kworker/u16:7: 0% user + 1.8% kernel
      1.7% 17685/logcat: 0.4% user + 1.3% kernel / faults: 33 minor
      1.6% 711/powerlogd: 1% user + 0.5% kernel / faults: 3 minor
      1.5% 540/icc_shared: 0% user + 1.5% kernel
      1.5% 2152/com.huawei.systemserver: 0.8% user + 0.6% kernel / faults: 805 minor
      1.4% 282/irq/213-thp: 0% user + 1.4% kernel
      1.4% 478/servicemanager: 0.5% user + 0.8% kernel / faults: 29 minor
      1.3% 2165/com.huawei.iaware: 0.9% user + 0.4% kernel / faults: 1864 minor
      1.3% 877/hwpged: 0.2% user + 1% kernel / faults: 6 minor
      1.2% 98/sys_heap: 0% user + 1.2% kernel
      1.2% 5807/usap64: 0.6% user + 0.5% kernel / faults: 1607 minor
      1.1% 840/displayengineserver: 0.5% user + 0.6% kernel / faults: 82 minor
      1.1% 917/hiview: 0.2% user + 0.9% kernel / faults: 1029 minor 5 major
      1.1% 16453/kworker/u16:16: 0% user + 1.1% kernel
      1% 95/spi3: 0% user + 1% kernel
      1% 858/CameraDaemon: 0.3% user + 0.6% kernel / faults: 5435 minor
      0.9% 689/[email protected]: 0% user + 0.8% kernel / faults: 25 minor
      0.9% 2141/com.huawei.hiview: 0.6% user + 0.3% kernel / faults: 927 minor
      0.8% 179/kswapd0: 0% user + 0.8% kernel
      0.7% 7880/chargelogcat-c: 0% user + 0.7% kernel / faults: 41 minor
      0.7% 28696/usap64: 0.3% user + 0.3% kernel / faults: 655 minor
      0.6% 24391/usap32: 0.4% user + 0.2% kernel / faults: 260 minor
      0.6% 9/rcu_preempt: 0% user + 0.6% kernel
      0.6% 17627/usap64: 0.4% user + 0.1% kernel / faults: 1169 minor
      0.5% 834/[email protected]: 0.2% user + 0.3% kernel / faults: 2832 minor
      0.5% 946/[email protected]: 0% user + 0.5% kernel / faults: 109 minor
      0.4% 703/lmkd: 0% user + 0.4% kernel / faults: 23 minor
      0.4% 3284/sugov:0: 0% user + 0.4% kernel
      0.4% 24402/usap32: 0.2% user + 0.2% kernel / faults: 718 minor 7 major
      0.4% 638/netd: 0% user + 0.3% kernel / faults: 1025 minor
      0.4% 692/[email protected]: 0% user + 0.3% kernel / faults: 46 minor
      0.4% 4158/com.huawei.recsys: 0.3% user + 0% kernel / faults: 304 minor
      0.4% 6206/kworker/u17:2: 0% user + 0.4% kernel
      0.4% 15044/kworker/u17:3: 0% user + 0.4% kernel
      0.3% 1/init: 0.2% user + 0.1% kernel / faults: 75 minor
      0.3% 6217/usap32: 0.1% user + 0.2% kerne

分析: 这里可以看出是哪个Activity导致ANR,而且可以排除不是cpu负载过高引起的

  • 查看拉取跟踪信息文件(traces.txt)

Android 会在遇到 ANR 时存储跟踪信息。在较低的操作系统版本中,设备上只有一个 /data/anr/traces.txt 文件。在较新的操作系统版本中,有多个 /data/anr/anr_* 文件。

也可以使用设备上的“生成错误报告”开发者选项或开发机器上的 adb bugreport 命令

下面是我在华为手机上打印的traces.txt

"main" prio=5 tid=1 Blocked
  | group="main" sCount=1 dsCount=0 flags=1 obj=0x71df12f8 self=0x7e7de10800
  | sysTid=12750 nice=-10 cgrp=default sched=1073741825/1 handle=0x7f04c22ed0
  | state=S schedstat=( 912713528 81383848 2003 ) utm=67 stm=23 core=4 HZ=100
  | stack=0x7fc9a10000-0x7fc9a12000 stackSize=8192KB
  | held mutexes=
  at 包名support.network.f.a(HttpManager.java:-1)
  - waiting to lock <0x09bbae37> (a 包名support.network.f) held by thread 4
  at 包名support.network.c.b(MyHttpClient.java:3)
  at 包名c.a.a.a(AsyncPrice.java:56)
  at 包名model.account.fragment.AccountNewFragment.J(AccountNewFragment.java:5)
  at 包名model.account.fragment.AccountNewFragment.t(AccountNewFragment.java:69)
  at 包名model.base.fragment.a.onViewCreated(BaseFragment.java:5)
  at androidx.fragment.app.g.a(FragmentManager.java:236)
  at androidx.fragment.app.g.i(FragmentManager.java:6)
  at androidx.fragment.app.g.a(FragmentManager.java:330)
  at androidx.fragment.app.a.f(BackStackRecord.java:24)
  at androidx.fragment.app.g.a(FragmentManager.java:445)
  at androidx.fragment.app.g.b(FragmentManager.java:66)
  at androidx.fragment.app.g.c(FragmentManager.java:56)
  at androidx.fragment.app.g.p(FragmentManager.java:8)
  at androidx.fragment.app.g.b(FragmentManager.java:5)
  at 包名MainActivity$v.onNavigationItemSelected(MainActivity.java:75)
  at com.google.android.material.bottomnavigation.BottomNavigationView$1.onMenuItemSelected(BottomNavigationView.java:3)
  at androidx.appcompat.view.menu.h.dispatchMenuItemSelected(MenuBuilder.java:1)
  at androidx.appcompat.view.menu.k.g(MenuItemImpl.java:2)
  at androidx.appcompat.view.menu.h.performItemAction(MenuBuilder.java:4)
  at com.google.android.material.bottomnavigation.BottomNavigationMenuView$1.onClick(BottomNavigationMenuView.java:3)
  at android.view.View.performClick(View.java:7199)
  at android.view.View.performClickInternal(View.java:7173)
  at android.view.View.access$3500(View.java:824)
  at android.view.View$PerformClick.run(View.java:27623)
  at android.os.Handler.handleCallback(Handler.java:894)
  at android.os.Handler.dispatchMessage(Handler.java:100)
  at android.os.Looper.loop(Looper.java:213)
  at android.app.ActivityThread.main(ActivityThread.java:8201)
  at java.lang.reflect.Method.invoke(Native method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)

这里看出是在MyHttpClient中执行了一个请求导致的,刚开始怀疑是在主线程中执行了这个耗时的请求才导致的,后面查看代码并不是这样

是在一个线程池里面执行的这个请求,但是代码这里为这个线程加了锁,当这个请求一直没有返回结果的时候,从而导致主线程一直等待,没有响应

参考

Android ANR

你可能感兴趣的:(Android ANR问题分析总结)