一次应用anr的分析示例

本文通过一个实际案例分析了Android应用出现ANR的原因,主要是由于主线程中进行了耗时的数据库查询操作,导致应用无响应。解决方法包括将耗时任务移到子线程,避免UI线程阻塞,并合理使用Handler进行线程间交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前两天项目上说开发的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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值