Mobile Development 9 min read

Understanding ANR (Application Not Responding) in Android: Triggers, Monitoring Mechanisms, and Prevention Strategies

This article explains what ANR is, outlines its three trigger conditions, describes the system's ANR execution flow, compares FileObserver and WatchDog monitoring techniques, shows how to analyze ANR logs and main‑thread stack traces, and provides practical guidelines to avoid ANR in Android applications.

Qunar Tech Salon
Qunar Tech Salon
Qunar Tech Salon
Understanding ANR (Application Not Responding) in Android: Triggers, Monitoring Mechanisms, and Prevention Strategies

ANR (Application Not Responding) occurs when the Android system detects that an app has been unresponsive for a prolonged period, severely affecting user experience and therefore must be reported and resolved.

The necessary condition for ANR is main‑thread blockage, which can be triggered by any of three cases: (1) the main thread fails to finish input events within 5 seconds, (2) a Service blocks for 20 seconds, or (3) a foreground broadcast blocks for 10 seconds or a background broadcast for 60 seconds. In practice, the first case is the most common.

The ANR execution process consists of the following steps:

The system detects an ANR.

The Process sends Linux signal 3 to itself and other running processes.

Each process writes its information to /data/anr/traces.txt .

Logcat records the ANR details.

The process enters ANR state, allowing retrieval of ANR data.

An ANR dialog is displayed.

The dialog disappears and the process returns to normal.

Because writing to /data/anr/traces.txt can take several seconds, the total time from ANR trigger to dialog display is typically around 10 seconds, varying by ROM.

ANR Monitoring Mechanisms

1. FileObserver monitors changes in the /data/anr directory to detect ANR occurrences. It provides the main‑thread stack trace and ANR information with minimal performance impact, but it cannot monitor most devices running Android 5.0 and above.

2. WatchDog runs a background thread that periodically (every 5 seconds) pings the main thread; if the main thread does not respond within the interval, an ANR is assumed. This works on all device models but introduces a small overhead.

The combined approach uses FileObserver on Android 5.0‑ and below, and on newer versions it first attempts FileObserver; if the /data/anr events are not observable, it falls back to WatchDog.

Log Analysis

ANR logs contain the affected process and component, ANR type, and CPU metrics (load averages, per‑state CPU time, major/minor page faults). High CPU usage or a single process consuming excessive CPU often indicates the root cause of ANR.

Main‑Thread Stack Information

The stack trace can be obtained with:

Thread mainThread = Looper.getMainLooper().getThread();

StackTraceElement[] mainStackTrace = mainThread.getStackTrace();

The bottom of the stack shows the process entry point ( ActivityThread.main ), while the top shows the currently executing method, helping developers locate time‑consuming operations in their code.

How to Avoid ANR

Avoid performing network, database, or heavy computation on the main thread.

Do not use Thread.wait() or Thread.sleep() on the main thread; use AsyncTask, Handler, or other asynchronous frameworks.

Use IntentService for background work.

Lower the priority of background threads to give the main thread more CPU time.

QACR Platform

The QACR platform provides monitoring for Java exceptions, native crashes, and ANR stalls in Android apps, reporting them to a centralized dashboard with separate environments for release and beta builds.

monitoringperformanceAndroidANRwatchdogFileObserver
Qunar Tech Salon
Written by

Qunar Tech Salon

Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.