1 anr 类型
ANR: Application Not Responding
Include 3 types:
1.1 Key Dispatching timeout ANR(5s)
The input event don’t responding in specified time which is defined in ActivityManagerService.java such as 5 second and then ANR happened. At this time the main thread must been blocked, maybe it try to do a network access such as check update or send http request in ui thread, or do database operation or do some work like image convert.
How to solve the problem? Maybe you need a dependent thread to do these time consuming work or use handler to do connection work between UI thread and other threads. And avoided the use sleep(), wait() api or synchronized methods in main thread.
1.2 Broadcast timeout ANR(10s)
The main thread can’t execute the OnReceive function on specified time such as 10s for foreground application and 20s for background application.
1.3 Service execution timeout ANR(20s)
In this case, the anr happened means that the main thread can’t execute the OnCreate or On StartCommand of Service finish in 20s
2 Main thread
include:
1). Activity:onCreate(), onResume(), onDestroy(), onKeyDown(), onClick(),etc
2). AsyncTask: onPreExecute(), onProgressUpdate(), onPostExecute(), onCancel,etc
3). Mainthread handler: handleMessage(), post*(runnable r), etc
4.) other
3 Debug SOP
I always follow the steps to debug ANR issue.
1) get the lagcat information and focus on the log that 10 seconds before ANR was happened.
2) Analysis the call stack from trace.txt
3) Get the real reason of ANR, such as iowait?Block?Memoryleak?
4) Review the relative code
4 ANR Case
4.1 Case 1:
ContentResolver in AsyncTask onPostExecute, high iowait
Do database operation
Process:com.android.email Activity:com.android.email/.activity.MessageView Subject:keyDispatchingTimedOut CPU usage from 2550ms to -2814ms ago: 5%187/system_server: 3.5% user + 1.4% kernel / faults: 86 minor 20major 4.4% 1134/com.android.email: 0.7% user + 3.7% kernel /faults: 38 minor 19 major 4% 372/com.android.eventstream: 0.7%user + 3.3% kernel / faults: 6 minor 1.1% 272/com.android.phone:0.9% user + 0.1% kernel / faults: 33 minor 0.9%252/com.android.systemui: 0.9% user + 0% kernel 0%409/com.android.eventstream.telephonyplugin: 0% user + 0% kernel /faults: 2 minor 0.1% 632/com.android.devicemonitor: 0.1% user + 0%kernel 100%TOTAL: 6.9% user + 8.2% kernel +84%iowait
-----pid 1134 at 2010-12-17 17:46:51 ----- Cmd line:com.android.email
DALVIK THREADS: (mutexes: tll=0 tsl=0tscl=0 ghl=0 hwl=0 hwll=0) "main" prio=5 tid=1 WAIT |group="main" sCount=1 dsCount=0 obj=0x2aaca180self=0xcf20 | sysTid=1134 nice=0 sched=0/0 cgrp=[fopen-error:2]handle=1876218976 at java.lang.Object.wait(Native Method) -waiting on <0x2aaca218> (a java.lang.VMThread) atjava.lang.Thread.parkFor(Thread.java:1424) atjava.lang.LangAccessImpl.parkFor(LangAccessImpl.java:48) atsun.misc.Unsafe.park(Unsafe.java:337) atjava.util.concurrent.locks.LockSupport.park(LockSupport.java:157) atjava.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:808) atjava.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:841) atjava.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1171) atjava.util.concurrent.locks.ReentrantLock$FairSync.lock(ReentrantLock.java:200) atjava.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:261) atandroid.database.sqlite.SQLiteDatabase.lock(SQLiteDatabase.java:378) atandroid.database.sqlite.SQLiteCursor.<init>(SQLiteCursor.java:222) atandroid.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:53) atandroid.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1356) atandroid.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1235) atandroid.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1189) atandroid.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1271) atcom.android.email.provider.EmailProvider.query(EmailProvider.java:1098) atandroid.content.ContentProvider$Transport.query(ContentProvider.java:187) atandroid.content.ContentResolver.query(ContentResolver.java:268) atcom.android.email.provider.EmailContent$Message.restoreMessageWithId(EmailContent.java:648) atcom.android.email.Controller.setMessageRead(Controller.java:658) atcom.android.email.activity.MessageView.onMarkAsRead(MessageView.java:700) atcom.android.email.activity.MessageView.access$2500(MessageView.java:98) atcom.android.email.activity.MessageView$LoadBodyTask.onPostExecute(MessageView.java:1290) atcom.android.email.activity.MessageView$LoadBodyTask.onPostExecute(MessageView.java:1255) atandroid.os.AsyncTask.finish(AsyncTask.java:417) atandroid.os.AsyncTask.access$300(AsyncTask.java:127) atandroid.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429) atandroid.os.Handler.dispatchMessage(Handler.java:99) atandroid.os.Looper.loop(Looper.java:123) atandroid.app.ActivityThread.main(ActivityThread.java:3652) atjava.lang.reflect.Method.invokeNative(Native Method) atjava.lang.reflect.Method.invoke(Method.java:507) atcom.android.internal.os.ZygoteIn
|
4.2 Case 2
R/W internet data block the ui thread
Set timeout or move the network operate to a new thread
ANRin process: com.android.mediascape:PhotoViewer (last incom.android.mediascape:PhotoViewer) Annotation:keyDispatchingTimedOut CPU usage: Load: 6.74 / 6.89 / 6.12 CPUusage from 8254ms to 3224ms ago: ovider.webmedia: 4% = 4% user +0% kernel / faults: 68 minor system_server: 2% = 1% user + 0%kernel / faults: 18 minor re-initialized>: 0% = 0% user + 0%kernel / faults: 50 minor events/0: 0% = 0% user + 0%kernel TOTAL:7% = 6% user + 1% kernel
DALVIKTHREADS: ""main"" prio=5 tid=3 NATIVE |group=""main"" sCount=1 dsCount=0 s=Yobj=0x4001b240 self=0xbda8 | sysTid=2579 nice=0 sched=0/0cgrp=unknown handle=-1343993184 atorg.apache.harmony.luni.platform.OSNetworkSystem.receiveStreamImpl(NativeMethod) atorg.apache.harmony.luni.platform.OSNetworkSystem.receiveStream(OSNetworkSystem.java:478) atorg.apache.harmony.luni.net.PlainSocketImpl.read(PlainSocketImpl.java:565) atorg.apache.harmony.luni.net.SocketInputStream.read(SocketInputStream.java:87) atorg.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection$LimitedInputStream.read(HttpURLConnection.java:303) atjava.io.InputStream.read(InputStream.java:133) atjava.io.BufferedInputStream.fillbuf(BufferedInputStream.java:157) atjava.io.BufferedInputStream.read(BufferedInputStream.java:346) atandroid.graphics.BitmapFactory.nativeDecodeStream(Native Method) atandroid.graphics.BitmapFactory.decodeStream(BitmapFactory.java:459) atcom.android.mediascape.activity.PhotoViewerActivity.getPreviewImage(PhotoViewerActivity.java:4465) atcom.android.mediascape.activity.PhotoViewerActivity.dispPreview(PhotoViewerActivity.java:4406) atcom.android.mediascape.activity.PhotoViewerActivity.access$6500(PhotoViewerActivity.java:125) atcom.android.mediascape.activity.PhotoViewerActivity$33$1.run(PhotoViewerActivity.java:4558) atandroid.os.Handler.handleCallback(Handler.java:587) atandroid.os.Handler.dispatchMessage(Handler.java:92) atandroid.os.Looper.loop(Looper.java:123) atandroid.app.ActivityThread.main(ActivityThread.java:4370) atjava.lang.reflect.Method.invokeNative(Native Method) atjava.lang.reflect.Method.invoke(Method.java:521) atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) atcom.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) atdalvik.system.NativeStart.main(Native Method) |
4.3 Case 3
Memeryleak: createbitmap that case the gc happened and then ANR