现在的工作基本上是每天分析各种各样的bug,是时候需要来整理一下了。
一、log中bug类型
1、程序异常强制关闭:force close
2、异常退出:uncaused exception
3、程序无相应:ANR
ANR类型:1) InputDispatchingTimedOut。主线程在5秒内没有完成用户的input事件(比如按键事件、屏幕触摸事件)
2) Service Timeout 。应用程序没有执行完成service的bind/create/start/destroy/unbind操作,前台服务20秒超时,后台服务200秒超时
3) Broadcast Timeout 。应用程序在规定时间内没有执行完成onReceive操作。前台广播10秒超时,后台广播60秒超时(BROADCAST_FG_TIMEOUT /BROADCAST_FG_TIMEOUT)
二、log日志中bug的查找和分析
1、force close
定位FATAL。
常见的FC日志:
1)空指针——NullPointException
————Attempt to invoke virtual method ......on a null object reference
解决方法:对应的位置加非空判断
2)安全异常——SecurityException
————java.lang.RuntimeException: Unable to resume activity {com.android.settings/com.android.settings.DeviceAdminAdd}: java.lang.SecurityException: com.google.android.gms from uid 10091 not allowed to perform SYSTEM_ALERT_WINDOW
解决方法:加上try {} catch (Exception e)
3)角标越界——IndexOutOfBoundsException、StringIndexOutOfBoundsException、ArrayIndexOutOfBoundsException
4)子线程不能修改UI: Only the original thread that created a view hierarchy can touch its views.
2、ANR
1)在logcat中搜索”ANR in”关键字,在这个log中可以发现ANR发生的时间,ANR发生的原因,CPU load信息和IO等信息
2)导出/data/anr目录下的traces文件, 在trace文件中包含ANR发生时, 进程中各个线程的堆栈情况。 这个目录下的traces文件对分析ANR现象非常有帮助。
建议:从LOG可以看出ANR的类型,CPU的使用情况,如果CPU使用量接近100%,说明当前设备很忙,有可能是CPU饥饿导致了ANR
如果CPU使用量很少,说明主线程被BLOCK了
如果IOwait很高,说明ANR有可能是主线程在进行I/O操作造成的
log分析:
13:12:14.123 I/Process( 220): Sending signal. PID: 21404 SIG: 3-----发生ANR的时间和生成trace.txt的时间
Reason:keyDispatchingTimedOut
04-0113:12:15.872 E/ActivityManager( 220): Load: 8.68 / 8.37 / 8.53-----5分钟,10分钟,15分钟的平均负载为8.68,8.37,8.53
04-0113:12:15.872 E/ActivityManager( 220): CPUusage from 4361ms to 699ms ago -----CPU在ANR发生前的使用情况
04-0113:12:15.872 E/ActivityManager( 220): CPUusage from 3697ms to 4223ms later-----ANR后CPU的使用量
Thread状态:
ZOMBIE ———— terminated thread
RUNNABLE ———— runnable or running now
TIMED_WAIT ———— timed waiting in Object.wait()
MONITOR ———— blocked on a monitor
WAIT ———— waiting in Object.wait()
INITIALIZING ———— allocated, not yet running
STARTING ———— started, not yet on thread list
NATIVE ———— off in a JNI native method
VMWAIT ———— waiting on a VM resource
SUSPENDED ———— suspended, usually by GC or debugger
UNKNOWN ———— thread is in the undefined state