Search code examples
androidlogcat

How to fix this AndroidANR?


TraceFile of mainThread:(This call stack is from system library, and the mainThread seems to be locked)

"main" prio=5 tid=1 Native
| group="main" sCount=1 dsCount=0 obj=0x7454c5c8 self=0xb4cb6500
| sysTid=11238 nice=0 cgrp=default sched=0/0 handle=0xb6f19b34
| state=S schedstat=( 111248118635 36668819001 451360 ) utm=7881 stm=3243 core=0 HZ=100
| stack=0xbe1a4000-0xbe1a6000 stackSize=8MB
| held mutexes=
kernel: (couldn't read /proc/self/task/11238/stack)
native: #00 pc 00017640  /system/lib/libc.so (syscall+28)
native: #01 pc 000f6e09  /system/lib/libart.so (_ZN3art17ConditionVariable4WaitEPNS_6ThreadE+96)
native: #02 pc 00291bb3  /system/lib/libart.so (_ZN3art3JNI16CallObjectMethodEP7_JNIEnvP8_jobjectP10_jmethodIDz+658)
native: #03 pc 00002bbb  /system/lib/libnativehelper.so (jniGetReferent+94)
native: #04 pc 000af741  /system/lib/libandroid_runtime.so (???)
native: #05 pc 00012e93  /system/lib/libutils.so (_ZN7android6Looper9pollInnerEi+530)
native: #06 pc 00012f63  /system/lib/libutils.so (_ZN7android6Looper8pollOnceEiPiS1_PPv+130)
native: #07 pc 00086b0d  /system/lib/libandroid_runtime.so (_ZN7android18NativeMessageQueue8pollOnceEP7_JNIEnvP8_jobjecti+22)
native: #08 pc 0000055d  /data/dalvik-cache/arm/system@[email protected] (Java_android_os_MessageQueue_nativePollOnce__JI+96)
at android.os.MessageQueue.nativePollOnce(Native method)
at android.os.MessageQueue.next(MessageQueue.java:323)
at android.os.Looper.loop(Looper.java:141)
at android.app.ActivityThread.main(ActivityThread.java:5653)
at java.lang.reflect.Method.invoke!(Native method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:746)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)

adbLog File of this ANR:

08-24 19:06:55.611  1751  1814 E ActivityManager: ANR in com.
08-24 19:06:55.611  1751  1814 E ActivityManager: PID: 11220
08-24 19:06:55.611  1751  1814 E ActivityManager: Reason: Input dispatching timed out (com, 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: 46.  Wait queue head age: 5508.2ms.)
08-24 19:06:55.611  1751  1814 E ActivityManager: Load: 11.81 / 11.91 / 11.85
08-24 19:06:55.611  1751  1814 E ActivityManager: CPU usage from 666215ms to 0ms ago:
08-24 19:06:55.611  1751  1814 E ActivityManager:   62% 562/sensors.qcom: 50% user + 11% kernel / faults: 198 minor
08-24 19:06:55.611  1751  1814 E ActivityManager:   9.5% 1751/system_server: 6.2% user + 3.3% kernel / faults: 5363 minor 2 major
08-24 19:06:55.611  1751  1814 E ActivityManager:   1.8% 1465/kworker/u:1: 0% user + 1.8% kernel
08-24 19:06:55.611  1751  1814 E ActivityManager:   1.7% 30539/kworker/u:5: 0% user + 1.7% kernel
08-24 19:06:55.611  1751  1814 E ActivityManager:   1.7% 10878/kworker/u:3: 0% user + 1.7% kernel
08-24 19:06:55.611  1751  1814 E ActivityManager:   1.7% 4805/kworker/u:2: 0% user + 1.7% kernel
08-24 19:06:55.611  1751  1814 E ActivityManager:   1.6% 12028/com.navi.tracker:remote: 1.3% user + 0.3% kernel / faults: 3968 minor
08-24 19:06:55.611  1751  1814 E ActivityManager:   1% 347/mediaserver: 0.2% user + 0.8% kernel
08-24 19:06:55.611  1751  1814 E ActivityManager:   0.9% 4977/mpdecision: 0% user + 0.8% kernel
08-24 19:06:55.611  1751  1814 E ActivityManager:   0.8% 222/logd: 0.5% user + 0.3% kernel / faults: 19 minor
08-24 19:06:55.611  1751  1814 E ActivityManager:   0.6% 7/kworker/u:0H: 0% user + 0.6% kernel
08-24 19:06:55.611  1751  1814 E ActivityManager:   0.6% 208/ueventd: 0.2% user + 0.4% kernel
08-24 19:06:55.611  1751  1814 E ActivityManager:   0.6% 427/irq/33-cpubw_hw: 0% user + 0.6% kernel
08-24 19:06:55.611  1751  1814 E ActivityManager:   0.6% 11973/com.navi.tracker: 0.4% user + 0.2% kernel / faults: 1345 minor
08-24 19:06:55.611  1751  1814 E ActivityManager:   0.6% 3745/com.android.phone: 0.4% user + 0.2% kernel / faults: 2342 minor
08-24 19:06:55.611  1751  1814 E ActivityManager:   0.5% 2791/com.android.systemui: 0.3% user + 0.1% kernel / faults: 973 minor
08-24 19:06:55.611  1751  1814 E ActivityManager:   0.5% 276/surfaceflinger: 0.2% user + 0.3% kernel / faults: 5 minor
08-24 19:06:55.611  1751  1814 E ActivityManager:   0.5% 2747/MC_Thread: 0% user + 0.5% kernel
08-24 19:06:55.611  1751  1814 E ActivityManager:   0.4% 3543/com.xiaomi.metoknlp: 0.3% user + 0.1% kernel / faults: 1652 minor
08-24 19:06:55.611  1751  1814 E ActivityManager:   0.3% 3/ksoftirqd/0: 0% user + 0.3% kernel
08-24 19:06:55.611  1751  1814 E ActivityManager:   0.3% 163/cfinteractive: 0% user + 0.3% kernel
08-24 19:06:55.611  1751  1814 E ActivityManager:   0.3% 26051/kworker/3:1H: 0% user + 0.3% kernel

I suspect that the cpu is overloaded, Has anyone seen a similar problem? Please give your help, thanks.


Solution

  • ***Please check your code you are doing anything heavy task on main UI thread that blocks UI and causes "Application Not Responding" that seems your device hanged.

    You can do heavy task on background thread to avoid ANR.

    Please go through below notes***

    Generally, the system displays an ANR if an application cannot respond to user input. For example, if an application blocks on some I/O operation (frequently a network access) on the UI thread so the system can't process incoming user input events. Or perhaps the app spends too much time building an elaborate in-memory structure or computing the next move in a game on the UI thread. It's always important to make sure these computations are efficient, but even the most efficient code still takes time to run.

    In any situation in which your app performs a potentially lengthy operation, you should not perform the work on the UI thread, but instead create a worker thread and do most of the work there. This keeps the UI thread (which drives the user interface event loop) running and prevents the system from concluding that your code has frozen. Because such threading usually is accomplished at the class level, you can think of responsiveness as a class problem. (Compare this with basic code performance, which is a method-level concern.)

    In Android, application responsiveness is monitored by the Activity Manager and Window Manager system services. Android will display the ANR dialog for a particular application when it detects one of the following conditions:

    No response to an input event (such as key press or screen touch events) within 5 seconds. A BroadcastReceiver hasn't finished executing within 10 seconds.

    How to avoid ANRs

    Android applications normally run entirely on a single thread by default the "UI thread" or "main thread"). This means anything your application is doing in the UI thread that takes a long time to complete can trigger the ANR dialog because your application is not giving itself a chance to handle the input event or intent broadcasts.

    Therefore, any method that runs in the UI thread should do as little work as possible on that thread. In particular, activities should do as little as possible to set up in key life-cycle methods such as onCreate() and onResume(). Potentially long running operations such as network or database operations, or computationally expensive calculations such as resizing bitmaps should be done in a worker thread (or in the case of databases operations, via an asynchronous request).

    The most effective way to create a worker thread for longer operations is with the AsyncTask class. Simply extend AsyncTask and implement the doInBackground() method to perform the work. To post progress changes to the user, you can call publishProgress(), which invokes the onProgressUpdate() callback method. From your implementation of onProgressUpdate() (which runs on the UI thread), you can notify the user.