Skip to content

Most visited

Recently visited

navigation

ANR Rate

When the UI thread of an Android app is blocked for too long, an "Application Not Responding" (ANR) error is triggered. If the app is in the foreground, the system displays a dialog to the user, as shown in figure 1. The ANR dialog gives the user the opportunity to force quit the app.

Figure 1. ANR dialog displayed to the user

Figure 1. ANR dialog displayed to the user

ANRs are a problem because the app’s main thread, which is responsible for updating the UI, can’t process user input events or draw, causing frustration to the user. For more information on the app’s main thread, see Processes and Threads.

An ANR will be triggered for your app when one of the following conditions occur:

To help improve the quality of your app, Android monitors when ANRs occur and reports occurrences in Android vitals. For information on how the data is collected, see Play Console docs.

If your app is experiencing ANRs, you can use the guidance in this article to diagnose and fix the problem.

Identify the problem

There are some common patterns to look for when diagnosing ANRs:

  1. The app is doing slow operations involving I/O on the main thread.
  2. The app is doing a long calculation on the main thread.
  3. The main thread is doing a synchronous binder call to another process, and that other process is taking a long time to return.
  4. The main thread is blocked waiting for a synchronized block for a long operation that is happening on another thread.
  5. The main thread is in a deadlock with another thread, either in your process or via a binder call. The main thread is not just waiting for a long operation to finish, but is in a deadlock situation. For more information, see Deadlock on Wikipedia.

The following techniques can help you find out which of these causes is causing your ANRs.

Strict mode

Using StrictMode helps you find accidental I/O operations on the main thread while you’re developing your app. You can use StrictMode at the application or activity level.

Enable background ANR dialogs

Android shows ANR dialogs for apps that take too long to process the broadcast message only if Show all ANRs is enabled in the device’s Developer options. For this reason, background ANR dialogs are not always displayed to the user, but the app could still be experiencing performance issues.

Traceview

You can use Traceview to get a trace of your running app while going through the use cases and identify the places where the main thread is busy. For information about how to use Traceview, see Profiling with Traceview and dmtracedump.

Pull a traces file

Android stores some trace information when it experiences an ANR in the /data/anr/traces.txt file on the device. You can get the file from an emulator by starting a shell session on the device as the root user, as shown in the following command-line example:

adb root
adb shell
cat /data/anr/traces.txt

You can capture a bug report from a physical device by using either the Take bug report developer option on the device, or the adb bugreport command on your development machine. For more information, see Capture and read bug reports.

Fix the problem

After you have identified the problem, you can use the tips in this section to fix commonly found problems.

Slow code on the main thread

Identify the places in your code where the app’s main thread is busy for more than 5 seconds. Look for the suspicious use cases in your app and try to reproduce the ANR.

For example, figure 2 shows a Traceview timeline where the main thread is busy for more than 5 seconds.

Figure 2. Traceview timeline showing a busy main
thread

Figure 2. Traceview timeline showing a busy main thread

Figure 2 shows us that most of the offending code happens in the onClick(View) handler, as shown in the following code example:

@Override
public void onClick(View view) {
    // This task runs on the main thread.
    BubbleSort.sort(data);
}

In this case, you should move the work that runs in the main thread to a worker thread. The Android Framework includes classes that can help to move the task to a worker thread, for more information, see Helper classes for threading. The following code shows how to use AsyncTask helper class to process the task on a worker thread:

@Override
public void onClick(View view) {
   // The long-running operation is run on a worker thread
   new AsyncTask<Integer[], Integer, Long>() {
       @Override
       protected Long doInBackground(Integer[]... params) {
           BubbleSort.sort(params[0]);
       }
   }.execute(data);
}

Traceview shows that most of the code runs on a worker thread, as shown in figure 3. The main thread is available to respond to user events.

Figure 3. Traceview timeline showing the work handled by a worker
thread

Figure 3. Traceview timeline showing the work handled by a worker thread

IO on the main thread

Executing IO operations on the main thread is a common cause of slow operations on the main thread, which can cause ANRs. It’s recommended to move all IO operations to a worker thread, as shown in the previous section.

Some examples of IO operations are network and storage operations. For more information, see Performing network operations and Saving data.

Lock contention

In some scenarios, the work that causes the ANR is not directly executed on the app’s main thread. If a worker thread holds a lock on a resource that the main thread requires to complete its work, then an ANR might happen.

For example, figure 4 shows a Traceview timeline where most of the work is performed on a worker thread.

Figure 4. Traceview timeline that shows the work being executed on a worker
thread

Figure 4. Traceview timeline that shows the work being executed on a worker thread

But if your users are still experiencing ANRs, you should look at the status of the main thread in Android Device Monitor. Usually, the main thread is in the RUNNABLE state if it’s ready to update the UI and is generally responsive.

But if the main thread can’t resume execution, then it’s in the BLOCKED status and can’t respond to events. The status shows on Android Device Monitor as Monitor or Wait, as shown in figure 5.

Figure 5. Main thread in the Monitor
status

Figure 5. Main thread in the Monitor status

The following trace shows an app’s main thread that is blocked waiting for a resource:

...
AsyncTask #2" prio=5 tid=18 Runnable
  | group="main" sCount=0 dsCount=0 obj=0x12c333a0 self=0x94c87100
  | sysTid=25287 nice=10 cgrp=default sched=0/0 handle=0x94b80920
  | state=R schedstat=( 0 0 0 ) utm=757 stm=0 core=3 HZ=100
  | stack=0x94a7e000-0x94a80000 stackSize=1038KB
  | held mutexes= "mutator lock"(shared held)
  at com.android.developer.anrsample.BubbleSort.sort(BubbleSort.java:8)
  at com.android.developer.anrsample.MainActivity$LockTask.doInBackground(MainActivity.java:147)
  - locked <0x083105ee> (a java.lang.Boolean)
  at com.android.developer.anrsample.MainActivity$LockTask.doInBackground(MainActivity.java:135)
  at android.os.AsyncTask$2.call(AsyncTask.java:305)
  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
  at java.lang.Thread.run(Thread.java:761)
...

Reviewing the trace can help you locate the code that blocks the main thread. The following code is responsible for holding the lock that blocks the main thread in the previous trace:

@Override
public void onClick(View v) {
    // The worker thread holds a lock on lockedResource
   new LockTask().execute(data);

   synchronized (lockedResource) {
       // The main thread requires lockedResource here
       // but it has to wait until LockTask finishes using it.
   }
}

public class LockTask extends AsyncTask<Integer[], Integer, Long> {
   @Override
   protected Long doInBackground(Integer[]... params) {
       synchronized (lockedResource) {
           // This is a long-running operation, which makes
           // the lock last for a long time
           BubbleSort.sort(params[0]);
       }
   }
}

Another example is an app’s main thread that is waiting for a result from a worker thread, as shown in the following code:

de
public void onClick(View v) {
   WaitTask waitTask = new WaitTask();
   synchronized (waitTask) {
       try {
           waitTask.execute(data);
           // Wait for this worker thread’s notification
           waitTask.wait();
       } catch (InterruptedException e) {}
   }
}

class WaitTask extends AsyncTask<Integer[], Integer, Long> {
   @Override
   protected Long doInBackground(Integer[]... params) {
       synchronized (this) {
           BubbleSort.sort(params[0]);
           // Finished, notify the main thread
           notify();
       }
   }
}

There are some other situations that can block the main thread, including threads that use Lock, Semaphore, as well as a resource pool (such as a pool of database connections) or other mutual exclusion (mutex) mechanisms.

You should evaluate the locks that your app holds on resources in general, but if you want to avoid ANRs, then you should look at the locks held for resources required by the main thread.

Make sure that the locks are held for the least amount of time, or even better, evaluate whether the app needs the hold in the first place. If you are using the lock to determine when to update UI based on the processing of a worker thread, use mechanisms such as onProgressUpdate() and onPostExecute() to communicate between the worker and main threads.

Deadlocks

A deadlock occurs when a thread enters a waiting state because a required resource is held by another thread, which is also waiting for a resource held by the first thread. If the app's main thread is in this situation, ANRs are likely to happen.

Deadlocks are a well-studied phenomenon in computer science, and there are deadlock prevention algorithms that you can use to avoid deadlocks.

For more information, see Deadlock and Deadlock prevention algorithms on Wikipedia.

Slow broadcast receivers

Apps can respond to broadcast messages, such as enabling or disabling airplane mode or a change in connectivity status, by means of broadcast receivers. An ANR occurs when an app takes too long to process the broadcast message.

An ANR occurs in the following cases:

Your app should only perform short operations in the onReceive() method of a BroadcastReceiver. However, if your app requires more complex processing as a result of a broadcast message you should defer the task to an IntentService.

You can use tools like Traceview to identify if your broadcast receiver executes long-running operations on the app's main thread. For example, figure 6 shows the timeline of a broadcast receiver that processes a message on the main thread for approximately 100 seconds.

Figure 6. Traceview timeline showing the BroadcastReceiver work on the main
thread

Figure 6. Traceview timeline showing the BroadcastReceiver work on the main thread

This behavior can be caused by executing long-running operations on the onReceive() method of the BroadcastReceiver, as shown in the following example:

@Override
public void onReceive(Context context, Intent intent) {
    // This is a long-running operation
    BubbleSort.sort(data);
}

In situations like these, it’s recommended to move the long-running operation to an IntentService because it uses a worker thread to execute its work. The following code shows how to use an IntentService to process a long-running operation:

@Override
public void onReceive(Context context, Intent intent) {
    // The task now runs on a worker thread.
    Intent intentService = new Intent(context, MyIntentService.class);
    context.startService(intentService);
}

public class MyIntentService extends IntentService {
   @Override
   protected void onHandleIntent(@Nullable Intent intent) {
       BubbleSort.sort(data);
   }
}

As a result of using the IntentService, the long-running operation is executed on a worker thread instead of the main thread. Figure 7 shows the work deferred to the worker thread in the Traceview timeline.

Figure 7. Traceview timeline showing the broadcast message processed on a
worker thread

Figure 7. Traceview timeline showing the broadcast message processed on a worker thread

Your broadcast receiver can use goAsync() to signal the system that it needs more time to process the message. However, you should call finish() on the PendingResult object. The following example shows how to call finish() to let the system recycle the broadcast receiver and avoid an ANR:

final PendingResult pendingResult = goAsync();
new AsyncTask<Integer[], Integer, Long>() {
   @Override
   protected Long doInBackground(Integer[]... params) {
       // This is a long-running operation
       BubbleSort.sort(params[0]);
       pendingResult.finish();
   }
}.execute(data);

However, moving the code from a slow broadcast receiver to another thread and using goAsync() will not fix the ANR if the broadcast is in the background. The ANR timeout still applies.

For more information about ANRs, see Keeping your App Resposive. For more information about threads, see Threading Performance.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)