美文网首页知识点性能优化
记一次解决ANR问题的笔记

记一次解决ANR问题的笔记

作者: 饱醉豚我去年买了个表 | 来源:发表于2018-05-03 23:36 被阅读181次

    记录一次解决ANR问题的笔记:

    起因:

    在项目提测之后,QA同学提了一个ANR的bug,然后按照她的步骤走了一遍,发现没有复现,所以怀疑是机型或者是账号的问题,于是把QA同学的手机借来又试了一遍,还是没有复现,于是怀疑跟账号有关系(APP需要登录),于是把她的账号也要过来又走了一遍,果然多次来回点击,若干次重复操作后就ANR了,既然出现了ANR,就来排查trace文件吧

    排查:

    1、先来看看AS中的Log日志:

    ActivityManager: ANR in com.xxxx.demo (ANR in com.xxxx.demo/.activity.MainActivity)   
    PID: 32265 
    
    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 = 27, head.seq = 2631206, Wait queue head age: 8508.5ms.)
    
    Load: 47.49 / 46.52 / 46.17 CPU usage from 3742ms to -4031ms ago (2018-05-03 17:00:43.398 to 2018-05-03 17:00:51.171):
     ....其他....                                              
    

    当Activity处于前台,APP在5秒内没有响应点击触摸事件时,就会产生ANR,通过LOG可以知道我们就是这种原因导致的ANR,那么具体是哪里的引起的这个问题呢?我们接着去trace文件中找答案。

    2、导出trace.txt:

    在AS中将下面的控制台切换到Terminal控制台,输入下面的命令:

    adb pull /data/anr/traces.txt /document/trace
    

    上述命令是将/data/anr目录下的trace文件导出到/document/trace目录下,回车,控制台输出:

    /data/anr/traces.txt: 1 file pulled. 16.4 MB/s (312500 bytes in 0.018s)
    

    说明导出成功,去相应目录下找到我们的traces.txt文件。

    3、查看trace文件:

    ----- pid 8939 at 2018-05-03 17:18:51 -----
    Cmd line: com.xxxx.demo //包名 及ANR发生时间
    Build fingerprint: 'HUAWEI/ALP-AL00/HWALP:8.0.0/HUAWEIALP-AL00/128(SP2C00):user/release-keys'
    ABI: 'arm64'
    Build type: optimized
    Zygote loaded classes=4756 post zygote classes=1856
    Intern table: 47739 strong; 137 weak
    JNI: CheckJNI is off; globals=7185 (plus 37 weak)
    
    .....一些堆栈信息.....
    
    "main" prio=5 tid=1 Native
      | group="main" sCount=1 dsCount=0 flags=1 obj=0x72f1bb10 self=0x793e0a3a00
      | sysTid=8939 nice=-10 cgrp=default sched=0/0 handle=0x7942f6a9b0
      | state=S schedstat=( 6206740125 206669232 6096 ) utm=562 stm=57 core=0 HZ=100
      | stack=0x7fd32ea000-0x7fd32ec000 stackSize=8MB
      | held mutexes=
      kernel: __switch_to+0xac/0xc0
      kernel: futex_wait_queue_me+0x100/0x1a0
      kernel: futex_wait+0x104/0x23c
      kernel: do_futex+0x16c/0x534
      kernel: SyS_futex+0x128/0x1bc
      kernel: __sys_trace_return+0x0/0x4
      native: #00 pc 000000000001da2c  /system/lib64/libc.so (syscall+28)
      native: #01 pc 0000000000066040  /system/lib64/libc.so (pthread_cond_wait+96)
      native: #02 pc 000000000004d5f8  /system/lib64/libhwui.so (???)
      native: #03 pc 00000000002c88bc  /system/framework/arm64/boot-framework.oat (Java_android_view_ThreadedRenderer_nSyncAndDrawFrame__J_3JI+188)
      at android.view.ThreadedRenderer.nSyncAndDrawFrame(Native method)
      at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:829)
      at android.view.ViewRootImpl.draw(ViewRootImpl.java:3178)
      at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2976)
      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2505)
      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1512)
      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7218)
      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:981)
      at android.view.Choreographer.doCallbacks(Choreographer.java:790)
      at android.view.Choreographer.doFrame(Choreographer.java:721)
      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:967)
      at android.os.Handler.handleCallback(Handler.java:808)
      at android.os.Handler.dispatchMessage(Handler.java:101)
      at android.os.Looper.loop(Looper.java:166)
      at android.app.ActivityThread.main(ActivityThread.java:7425)
      at java.lang.reflect.Method.invoke(Native method)
      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
    

    重点看下面这几行:

    android.view.ThreadedRenderer.draw(ThreadedRenderer.java:829)
    at android.view.ViewRootImpl.draw(ViewRootImpl.java:3178)
    at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2976)
    

    OK,虽然没有具体定位到哪一行代码,但是根据ANR出现的时机以及复现条件大概推断出是哪里出了问题,最后发现是自定义View的onDraw()中漏判了一个条件导致无限循环了,OK,到这里就把问题解决了,把这个问题记录下来方便以后来查询,也提醒自己要再细心一些~

    相关网址:
    [1]:https://developer.android.com/topic/performance/vitals/anr

    相关文章

      网友评论

        本文标题:记一次解决ANR问题的笔记

        本文链接:https://www.haomeiwen.com/subject/zarrrftx.html