What is ANR
ANR = Application Not Responding
ANR type:
- Key Dispatch Timeout
Not response to an input event(eg: key press,screen touch) within (15 seconds or 8 seconds,default 5 seconds)
- Broadcast Timeout
a broadcastReceiver hasn’t finished within 10 seconds
- Service Timeout
Request service failed within 20 seconds
Reminder
- if you choose “force to close ”,AMS will kill the process.
- Keep the scene for get important info is very import.
ANR Essential:
Java process has main thread and main message queue(main thread = activity thread)
The Main Thread ,that is responsible for handling the ui events like Draw,Listen and receive the UI events.
Main thread will pick up message from main message queue and dispatch it as soon as possible.
Before main thread finish processing current message ,it can not pick up next one.
If main thread stuck with one message and fail to dispatch is in time, ANR will occur.
ANR key info
We need to know why main thread can not process the message in time?
- Can not get CPU time to run
- wait for some event but it donot happen in time?
- Message handle flow is too complicated?
So need the follow files for analyze.
- MTKLog saved in T card,include:
Aee_exp->db files(data/aee_exp)
MoblieLog->APLog_xxx_xxx->APLog
MdLog->MDLog_xxx_xxx->Modem Log
Netlog-> MDLog_xxx_xxx->Network Log
- data/anr saved on the phone,include:
Trace.txt
Mobile Log:
you can get more info about anr from event log and main log:
- The ANR time stamp
- which process happens anr
- which type of anr
- more inof about the process before anr
LOG中搜索关键字:
anr
application is not responding
system_app_anr
Trace.txt
you can get callstack from the Trace.txt
Analyze ANR
在APLOG中搜索关键字:
anr
application is not responding
Example
- Description:
IDLE界面进入图库,打开大量的图片,选择条目,点击几张图片,按HOME键,再从IDLE界面进入图库,点击图片,弹出图库没有响应。
- Analyze:
(1)find anr time,process ID,ANT type
在APLOG中搜索关键字:
anr
application is not responding
system_app_anr
(2)Check CPU usage
(3)check Trace.txt,mapping process id and time stamp.
最后发现是在主线程中执行了耗时操作query数据库,导致ANR
(4)find more info from main and event log to check main thread.
最后确认是:
main thread are busy query db database all the time.
Need MTK help
提供有效的LOG:
-MTKLog saved in T card,include:
Aee_exp->db files
MoblieLog->APLog_xxx_xxx->APLog
MdLog->MDLog_xxx_xxx->Modem Log
Netlog-> MDLog_xxx_xxx->Network Log
-data/anr saved on the phone,include:
Trace.txt
最好是提供ENG的LOG,ENG会提供了更多的信息。
常见导致ANR的原因:
- 数据库操作I/O被阻塞
iowait、ContentResolver.query、onPostExecute - 在UI线程进行网络数据的读写
- 内存不足导致block在创建bitmap上
atdalvik.system.VMRuntime.trackExternalAllocation(NativeMethod)
atandroid.graphics.Bitmap.createBitmap(Bitmap.java:468) - 程序异常退出,uncausedexception (Fatal)
- 程序强制关闭,ForceClosed (简称FC) (Fatal)
- 通过搜索ANR定位问题
解决ANR的方法:
- 不要让主线程干耗时的工作
- 不要让其他线程阻塞主线程的执行
参考资料
1.如何分析解决Android ANR
http://blog.csdn.net/dadoneo/article/details/8270107
2.Android ANR分析与总结
http://blog.csdn.net/androiddevelop/article/details/49515903
3.Android ANR分析
http://blog.csdn.net/yxz329130952/article/details/50087731
4.Application Not Responding(ANR) Analyze
MediaTek On-Line > eCourse Home > SW > ALPS > Common Framework