Understanding Android ANR (Application Not Responding) and a Comparative Review of Monitoring Solutions
This article explains the causes and trigger conditions of Android ANR, describes the underlying message‑driven mechanism, and compares four popular detection approaches—BlockCanary, ANR‑WatchDog, SafeLooper, and FileObserver—highlighting their advantages, limitations, and practical usage recommendations.
ANR (Application Not Responding) occurs when the main thread is blocked for too long, severely degrading user experience. Test engineers need a solid grasp of ANR principles to design effective monitoring strategies for various performance stalls.
Trigger conditions : ANR is essentially a performance issue caused by time‑consuming operations on the main thread. Common timeout thresholds are shown in the original diagram, and most observable ANRs stem from InputEvent handling, while Service and Broadcast only log traces without user prompts.
Android's monitoring mechanism : Android apps are driven by a message loop; the UI, events, and lifecycle are all tied to message handling. ANR detection typically leverages this mechanism, monitoring delays in message processing. InputEvent‑related ANRs are monitored natively but still block the main thread’s message queue.
Detection solutions :
BlockCanary : A lightweight, non‑intrusive component that hooks into Looper.loop log statements to measure the time between message dispatches. It can locate stalls and capture stack traces, but it fails to detect ANRs caused by InputEvent blocking, CPU‑resource starvation, or long‑running dispatches, and has compatibility issues with certain logger changes.
ANR‑WatchDog : Inspired by the system WatchDog, it runs a separate thread that increments a counter and sleeps; if the main thread does not increment the counter within a configurable threshold, an ANR is reported. It works across devices and requires no code changes, but its detection depends heavily on the chosen timeout and may miss ANRs with short stall durations.
SafeLooper : A more experimental approach that creates a blocking message and uses reflection to take over the main thread’s Looper. It offers high customizability but incurs significant performance overhead due to reflective message management.
FileObserver : Monitors changes in the /data/anr directory, which the ActivityManagerService updates when an ANR occurs. This method provides accurate timing and content, but its applicability is limited by SELinux restrictions on non‑system apps after Android 5.0, requiring root or elevated privileges for reliable operation.
Summary : The article consolidates the principles and implementations of the main ANR monitoring techniques. While no single solution covers all scenarios, a combination of FileObserver (for precise detection) and ANR‑WatchDog (for broad compatibility) is recommended to achieve comprehensive coverage across most devices.
References :
https://github.com/ACRA/acra
https://github.com/markzhai/AndroidPerformanceMonitor
https://github.com/SalomonBrys/ANR-WatchDog
http://gityuan.com/2016/07/02/android-anr/
http://blog.csdn.net/zhudaozhuan/article/details/50964832
360 Quality & Efficiency
360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.