• Android ANR详解


    如何避免KeyDispatchTimeout
    1:UI线程尽量只做跟UI相关的工作
    2:耗时的工作(比如数据库操作,I/O,连接网络或者别的有可能阻碍UI线程的操作)把它放入单独的线程处理
    3:尽量用Handler来处理UIthread和别的thread之间的交互
    

    如何去分析ANR

    DALVIK THREADS (22):
    "main" prio=5 tid=1 Native
      | group="main" sCount=1 dsCount=0 obj=0x74729000 self=0xf4827800
      | sysTid=29242 nice=0 cgrp=default sched=0/0 handle=0xf7779bec
      | state=D schedstat=( 1168866313 28837852 971 ) utm=90 stm=26 core=5 HZ=100
      | stack=0xff5f3000-0xff5f5000 stackSize=8MB
      | held mutexes=
      kernel: (couldn't read /proc/self/task/29242/stack)
      native: (backtrace::Unwind failed for thread 29242)
      at libcore.io.Posix.fsync(Native method)
      at libcore.io.BlockGuardOs.fsync(BlockGuardOs.java:143)
      at java.io.FileDescriptor.sync(FileDescriptor.java:74)
      at android.os.FileUtils.sync(FileUtils.java:152)
      at android.app.SharedPreferencesImpl.writeToFile(SharedPreferencesImpl.java:598)
      at android.app.SharedPreferencesImpl.access$800(SharedPreferencesImpl.java:51)
      at android.app.SharedPreferencesImpl$2.run(SharedPreferencesImpl.java:512)
      - locked <@addr=0x32c014d0> (a java.lang.Object)
      at android.app.SharedPreferencesImpl.enqueueDiskWrite(SharedPreferencesImpl.java:533)
      at android.app.SharedPreferencesImpl.access$100(SharedPreferencesImpl.java:51)
      at android.app.SharedPreferencesImpl$EditorImpl.commit(SharedPreferencesImpl.java:455)
      at com.zhangyue.iReader.DB.SPHelper.setString(SourceFile:56)
      - locked <@addr=0x32c01810> (a com.zhangyue.iReader.DB.SPHelper)
      at com.zhangyue.iReader.account.a.a(SourceFile:533)
      at com.zhangyue.iReader.account.a.b(SourceFile:526)
      at com.zhangyue.iReader.account.a.a(SourceFile:215)
      at com.zhangyue.iReader.app.APP.initAPP(SourceFile:681)
      at com.zhangyue.iReader.app.APP.initAPPData(SourceFile:731)
      at com.zhangyue.iReader.app.IreaderApplication.onCreate(SourceFile:53)
      at com.ireader.plug.utils.a.a(unavailable:-1)
      at com.ireader.plug.api.IreaderPlugApi.initPlugin(unavailable:-1)
      at com.android.browser.Browser.onCreate(SourceFile:77)
      at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1024)
      at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5084)
      at android.app.ActivityThread.access$1600(ActivityThread.java:187)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1646)
      at android.os.Handler.dispatchMessage(Handler.java:111)
      at android.os.Looper.loop(Looper.java:194)
      at android.app.ActivityThread.main(ActivityThread.java:5871)
      at java.lang.reflect.Method.invoke!(Native method)
      at java.lang.reflect.Method.invoke(Method.java:372)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1119)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)
    
    "Heap thread pool worker thread 0" prio=5 tid=2 Native (still starting up)
      | group="" sCount=1 dsCount=0 obj=0x0 self=0xeec4d400
      | sysTid=29250 nice=0 cgrp=default sched=0/0 handle=0xf5078000
      | state=S schedstat=( 616307 0 18 ) utm=0 stm=0 core=7 HZ=100
      | stack=0xf4390000-0xf4392000 stackSize=1020KB
      | held mutexes=
      kernel: (couldn't read /proc/self/task/29250/stack)
      native: #00 pc 000134a4  /system/lib/libc.so (syscall+28)
      native: #01 pc 000a9b27  /system/lib/libart.so (art::ConditionVariable::Wait(art::Thread*)+74)
      native: #02 pc 00237b0f  /system/lib/libart.so (art::ThreadPool::GetTask(art::Thread*)+50)
      native: #03 pc 00237ab9  /system/lib/libart.so (art::ThreadPoolWorker::Run()+52)
      native: #04 pc 002382e1  /system/lib/libart.so (art::ThreadPoolWorker::Callback(void*)+52)
      native: #05 pc 00016f97  /system/lib/libc.so (__pthread_start(void*)+30)
      native: #06 pc 00014f9f  /system/lib/libc.so (__start_thread+6)
      (no managed stack frames)
    

    拿到一个日志文件,要分成多段来看 。 log文件很长,其中包含十几个小单元信息,但不要被吓到 ,事实上他主要由三大块儿组成 。

    1,系统基本信息 ,包括 内存,CPU ,进程队列 ,虚拟内存 , 垃圾回收等信息 。------ MEMORY INFO (/proc/meminfo) ------
    ------ CPU INFO (top -n 1 -d 1 -m 30 -t) ------
    ------ PROCRANK (procrank) ------
    ------ VIRTUAL MEMORY STATS (/proc/vmstat) ------
    ------ VMALLOC INFO (/proc/vmallocinfo) ------

    格式如下:
    ------ MEMORY INFO (/proc/meminfo) ------
    MemTotal: 347076 kB
    MemFree: 56408 kB
    Buffers: 7192 kB
    Cached: 104064 kB
    SwapCached: 0 kB
    Active: 192592 kB
    Inactive: 40548 kB
    Active(anon): 129040 kB
    Inactive(anon): 1104 kB
    Active(file): 63552 kB
    Inactive(file): 39444 kB
    Unevictable: 7112 kB
    Mlocked: 0 kB
    SwapTotal: 0 kB
    SwapFree: 0 kB
    Dirty: 44 kB
    Writeback: 0 kB
    AnonPages: 129028 kB
    Mapped: 73728 kB
    Shmem: 1148 kB
    Slab: 13072 kB
    SReclaimable: 4564 kB
    SUnreclaim: 8508 kB
    KernelStack: 3472 kB
    PageTables: 12172 kB
    NFS_Unstable: 0 kB
    Bounce: 0 kB
    WritebackTmp: 0 kB
    CommitLimit: 173536 kB
    Committed_AS: 7394524 kB
    VmallocTotal: 319488 kB
    VmallocUsed: 90752 kB
    VmallocChunk: 181252 kB

    2,时间信息 , 也是我们主要分析的信息 。
    ------ VMALLOC INFO (/proc/vmallocinfo) ------
    ------ EVENT INFO (/proc/vmallocinfo) ------

    格式如下:
    ------ SYSTEM LOG (logcat -b system -v time -d *:v) ------
    01-15 16:41:43.671 W/PackageManager( 2466): Unknown permission com.wsomacp.permission.PROVIDER in package com.android.mms
    01-15 16:41:43.671 I/ActivityManager( 2466): Force stopping package com.android.mms uid=10092
    01-15 16:41:43.675 I/UsageStats( 2466): Something wrong here, didn’t expect com.sec.android.app.twlauncher to be paused
    01-15 16:41:44.108 I/ActivityManager( 2466): Start proc com.sec.android.widgetapp.infoalarm for service com.sec.android.widgetapp.infoalarm/.engine.DataService: pid=20634 uid=10005 gids={3003, 1015, 3002}
    01-15 16:41:44.175 W/ActivityManager( 2466): Activity pause timeout for HistoryRecord{48589868 com.sec.android.app.twlauncher/.Launcher}
    01-15 16:41:50.864 I/KeyInputQueue( 2466): Input event
    01-15 16:41:50.866 D/KeyInputQueue( 2466): screenCaptureKeyFlag setting 0
    01-15 16:41:50.882 I/PowerManagerService( 2466): Ulight 0->7|0
    01-15 16:41:50.882 I/PowerManagerService( 2466): Setting target 2: cur=0.0 target=70 delta=4.6666665 nominalCurrentValue=0
    01-15 16:41:50.882 I/PowerManagerService( 2466): Scheduling light animator!
    01-15 16:41:51.706 D/PowerManagerService( 2466): enableLightSensor true
    01-15 16:41:51.929 I/KeyInputQueue( 2466): Input event
    01-15 16:41:51.933 W/WindowManager( 2466): No focus window, dropping: KeyEvent{action=0 code=26 repeat=0 meta=0 scancode=26 mFlags=9}

    3,虚拟机信息 , 包括进程的,线程的跟踪信息,这是用来跟踪进程和线程具体点的好地方 。
    ------ VM TRACES JUST NOW (/data/anr/traces.txt.bugreport: 2011-01-15 16:49:02) ------
    ------ VM TRACES AT LAST ANR (/data/anr/traces.txt: 2011-01-15 16:49:02) ------

    格式如下 :
    ----- pid 21161 at 2011-01-15 16:49:01 -----
    Cmd line: com.android.mms
    
    DALVIK THREADS:
    "main" prio=5 tid=1 NATIVE
      | group="main" sCount=1 dsCount=0 s=N obj=0x4001d8d0 self=0xccc8
      | sysTid=21161 nice=0 sched=0/0 cgrp=default handle=-1345017808
      | schedstat=( 4151552996 5342265329 10995 )
      at android.media.MediaPlayer._reset(Native Method)
      at android.media.MediaPlayer.reset(MediaPlayer.java:1218)
      at android.widget.VideoView.release(VideoView.java:499)
      at android.widget.VideoView.access$2100(VideoView.java:50)
      at android.widget.VideoView$6.surfaceDestroyed(VideoView.java:489)
      at android.view.SurfaceView.reportSurfaceDestroyed(SurfaceView.java:572)
      at android.view.SurfaceView.updateWindow(SurfaceView.java:476)
      at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:206)
      at android.view.View.dispatchDetachedFromWindow(View.java:6082)
      at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1156)
      at android.view.ViewGroup.removeAllViewsInLayout(ViewGroup.java:2296)
      at android.view.ViewGroup.removeAllViews(ViewGroup.java:2254)
      at com.android.mms.ui.SlideView.reset(SlideView.java:687)
      at com.android.mms.ui.SlideshowPresenter.presentSlide(SlideshowPresenter.java:189)
      at com.android.mms.ui.SlideshowPresenter$3.run(SlideshowPresenter.java:531)
      at android.os.Handler.handleCallback(Handler.java:587)
      at android.os.Handler.dispatchMessage(Handler.java:92)
      at android.os.Looper.loop(Looper.java:123)
      at android.app.ActivityThread.main(ActivityThread.java:4627)
      at java.lang.reflect.Method.invokeNative(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:521)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
      at dalvik.system.NativeStart.main(Native Method)
    
    

    我总结了观察log文件的基本步骤 。
    1,如果是ANR问题 , 则搜索“ANR ”关键词 。 快速定位到关键事件信息 。
    2,如果是ForceClosed 和其它异常退出信息,则搜索"Fatal" 关键词, 快速定位到关键事件信息 。如果是crash,则搜索CRASH.
    3,定位到关键事件信息后 , 如果信息不够明确的,再去搜索应用程序包的虚拟机信息 ,查看具体的进程和线程跟踪的日志,来定位到代码 。 搜索 DALVIK THREADS ,查看tid=1的进程的情况
    4, 搜索你的包名,进程名等。

    用这种方法,出现问题,根本不需要断点调试 , 直接定位到问题,屡试不爽 。
    下面,我们就开始来分析这个例子的log 。

    打开log文件 , 由于是ANR错误,因此搜索"ANR " , 为何要加空格呢,你加上和去掉比较一下就知道了 。 可以屏蔽掉不少保存到anr.log文件的无效信息 。

    参考:http://blog.csdn.net/dadoneo/article/details/8270107

  • 相关阅读:
    Git入门
    基于sendmail的简单zabbix邮件报警
    zabbix agentd安装
    我整理的一份来自于线上的Nginx配置(Nginx.conf),希望对学习Nginx的有帮助
    【转载】Spring Boot引起的“堆外内存泄漏”排查及经验总结
    lodop+art-template实现web端漂亮的小票样式打印
    《阿里巴巴Java开发手册》改名《Java开发手册》,涵盖史无前例的三大升级
    Spring Boot的学习之路(02):和你一起阅读Spring Boot官网
    Spring Boot的学习之路(01):缘起
    『 效率工具 』Spring Boot版的轻量级代码生成器,减少70%以上的开发任务
  • 原文地址:https://www.cnblogs.com/caoxinyu/p/10568613.html
Copyright © 2020-2023  润新知