前两天项目上说开发的app在长时间运行之后出现卡顿和anr,
寻求解决方法。
ANR即Application Not Responding,
就是应用程序无响应。
主要是一些耗时操作比如网络请求或者IO操作,
造成主线程5s内不能响应用户事件,
或者BroadcastReceiver的onReceive方法执行时间超过10s,
就会显示ANR对话框提示用户应用ANR。
ANR一般有三种类型:
1. KeyDispatchTimeout(5 seconds) --主要类型按键或触摸事件在5s内无响应
2. BroadcastTimeout(10 seconds) --BroadcastReceiver在10s内无法处理完成
3. ServiceTimeout(20 seconds) --小概率类型 Service在20s内无法处理完成
要分析anr的具体原因就要看log,
于是拿了全部设备开始运行,
连上adb,
adb logcat > log.txt
噼里啪啦一顿操作,
测了四个小时,
界面终于开始不那么流畅了,
划划划,
终于anr了。。。
当ANR发生后,
系统自动生成/data/anr/traces.txt文件,
记录ANR时系统各个线程的执行状态。
adb pull /data/anr/traces.txt
将这个文件导出来。
先来看log,
搜索anr,
发现了这么一段,
06-18 14:08:16.477 1351 2907 E LocSvc_libulp: I/int ulp_brain_process_gnss_position_report(enum loc_sess_status, LocPosTechMask, UlpLocation *, const GpsLocationExtended *, const void *), transition provider returned = 0
06-18 14:08:16.477 1351 2907 E LocSvc_libulp: I/int ulp_brain_process_gnss_position_report(enum loc_sess_status, LocPosTechMask, UlpLocation *, const GpsLocationExtended *, const void *), report GNSS position/status to hybrid fix requests = 0
#06-18 14:08:16.663 1351 1800 I InputDispatcher: Application is not responding: Window{ff7ae49 u0 com.******.win/com.******.win.ProfessionalTest}. It has been 8003.0ms since event, 8002.7ms since wait started. Reason: 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: 30. Wait queue head age: 8502.7ms.
06-18 14:08:16.737 1351 1451 I Process : Sending signal. PID: 6350 SIG: 3
06-18 14:08:16.737 6350 6356 I art : Thread[3,tid=6356,WaitingInMainSignalCatcherLoop,Thread*=0xdb705d00,peer=0x12c060d0,"Signal Catcher"]: reacting to signal 3
06-18 14:08:16.738 6350 6356 I art :
06-18 14:08:16.809 570 663 E : iawared: change_property_content open /dev/iolimit/iocontrol/iolimit.switching fail errno(13)!
06-18 14:08:16.809 570 663 E : iawared: change_property_content open /dev/iolimit/heavyio/iolimit.switching fail errno(13)!
06-18 14:08:16.892 6350 6356 I art : Wrote stack traces to '/data/anr/traces.txt'
06-18 14:08:17.024 6350 6350 I s