欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android如何分析ANR问题

程序员文章站 2022-07-14 17:21:42
...

分析ANR步骤

确定ANR基本信息

先搜索am_anr 确定ANR时间点,进程id ,以及什么类型ANR,等待了多久等
03-04 13:45:03.781 1000 1199 14667 I am_anr : [0,2900,com.android.systemui,551042573**,Input dispatching timed ou**t (StatusBar, b49ba2b StatusBar (server) is not responding. Waited 8001ms for MotionEvent(action=DOWN))]

查看/data/anr下的traces文件

查看对应pid的traces,如果有明显的等锁耗时&主线程执行时间比较长,基本可以直接确定原因。

查看相应时间段内相关的log

搜索 “ANR in”往前时间点搜索pid 相关信息(在03-04 13:45:03.781 往前推8s时间内)

常见的ANR分析

app端执行某个消息耗时导致ANR

// io耗时导致110消息执行时间过长,导致Receiver消息迟迟得不到执行而发生ANR

06-30 15:11:11.411 1000 1895 8935 I **am_anr** : [0,5831,com.android.settings,952745541,Broadcast of Intent \{ act=android.hardware.usb.action.USB_STATE flg=0x31000010 cmp=com.android.settings/.connecteddevice.usb.UsbModeChooserReceiver (has extras) }]  
06-30 15:11:16.453 1000 5831 5831 I **am_lifecycle_sample**: [0,com.android.settings,110,**14998]**
06-30 15:11:16.454 1000 5831 5831 W Looper : PerfMonitor longMsg : seq=2 plan=15:11:01.454 late=2ms **wall=14998ms** running=77ms runnable=55ms **io=14144ms** h=android.app.ActivityThread$H w=110 procState=2
06-30 15:11:16.469 1000 5831 5831 D UsbModeChooserReceiver: action = android.hardware.usb.action.USB_STATE

app错误使用前台Service导致ANR

// 进程crash后不会立刻拉起Service,而是通过计算delay去启动,此处delay时间是6268ms,log中正好6s后才开始通过MxActivateService拉起短信进程

05-31 19:35:17.316 1760 8998 I am_proc_died: [0,27908,com.android.mms,200,4]
05-31 19:35:17.406 1760 8998 I am_schedule_service_restart: [0,com.android.mms/com.xiaomi.mms.transaction.MxActivateService,6268]
05-31 19:35:23.812 1760 1823 I am_proc_start: [0,16206,10104,com.android.mms,service,\\{com.android.mms/com.xiaomi.mms.transaction.MxActivateService}] 
05-31 19:35:24.031 1760 3001 I am_proc_bound:  [0,16206,com.android.mms]

// 这个ANR的超时时间是10s,计算时间起始点是system server端开始post app端的Service的onStartCommand要调用的时候。如果在onStartCommand开始执行后10s内未调用startForeground方法就会发生ANR。

05-31 19:35:24.500 1760 16269 I am_anr : [0,16206,com.android.mms,952745573,Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{3201640 u0 com.android.mms/com.xiaomi.mms.transaction.MxActivateService}]

// 正常使用startForeground方法大家基本会放在onStartCommdn方法里,而不是放在Service的onCreate方法中。因为如果你onStartCommand的返回值是START_NOT_STICKY,进程被杀后重启不会回调onCreate方法,但是还是会有还会走ANR的逻辑。

app自身代码问题导致主线程等子线程的锁导致ANR

02-07 16:24:51.941 1000 1237 8731 I am_anr : [0,1855,com.miui.home,953794117,Input dispatching timed out (com.miui.home/com.miui.home.recents.settings.NavigationBarTypeActivity, 1d271ca com.miui.home/com.miui.home.recents.settings.NavigationBarTypeActivity (server) is not responding. Waited 8000ms for FocusEvent(hasFocus=false))]

// 发生ANR的8s内,home主线程有等锁11s,导致input事件未被及时处理。

02-07 16:24:52.566 10074 1855 1855 I dvm_lock_sample: [com.miui.home,1,main,11259,Launcher.java,7312,void com.miui.home.launcher.Launcher.bindAppsChanged(java.util.List, java.util.List, java.util.List),LauncherModel.java,1468,void com.miui.home.launcher.LauncherModel$LoaderTask.run(),100]

进程A通过四大组件等方式拉起进程B时,进程B启动过程中被杀或耗时均可能导致ANR

// content provider拉起进程时,进程刚拉起尚未publish provider就被杀死,所以getContentProviderImpl 等待耗时,导致bindet耗时,app端子线程持锁耗时,主线程等锁耗时,从而发生ANR

06-23 21:00:05.484 1000 1606 1749 I am_proc_start: [0,15287,1000,com.miui.securitycenter.remote,content provider,\{com.miui.securitycenter/com.miui.idprovider.IdProvider}]
06-23 21:00:05.555 1000 1606 2891 I am_proc_bound: [0,15287,com.miui.securitycenter.remote]
06-23 21:00:05.656 1000 1606 2891 I am_kill : [0,15287,com.miui.securitycenter.remote,100,stop com.miui.securitycenter due to from process:com.miui.securitycenter.remote]
06-23 21:00:05.688 1000 1606 1748 I am_wtf : [0,6155,system_server,-1,ActivityManager,Timeout waiting for provider com.miui.securitycenter/1000 for provider com.miui.idprovider providerRunning=false caller=com.miui.systemAdSolution/10111]
06-23 21:00:16.592 10102 15899 15950 W BpBinder: PerfMonitor binderTransact : time=5464ms interface=android.app.IActivityManager code=22
06-23 21:00:16.591 10153 3966 4704 W BpBinder: PerfMonitor binderTransact : time=10529ms interface=android.app.IActivityManager code=22
06-23 21:00:16.592 10153 3966 4704 E ActivityThread: Failed to find provider info for com.miui.idprovider
相关标签: AMS android java