Application Not Responding error

“He seems to be completely unreceptive
The tests I gave him showed no sense at all.”

This app deliberately causes the Application Not Responding error. Follow the directions below.

Every app is born with exactly one thread, called the main thread or the UI thread. The UI thread must continually respond to touches and other incoming events. If the UI thread gets bogged down with a task that would take more than a few seconds, the thread would be uable to give the touches the prompt attention they deserve. The app would become unresponsive, and we would get the ANR dialog box. unresp

When you press the button, the UI thread of this app will enter a loop that will take a full 10 seconds. The purpose of this loop is to deliberately cause the ANR error message.

The classic examples of time consuming tasks are

  1. a network download
  2. a database lookup
  3. an infinite loop, as in Pong and Pearl.
When faced with one of these tasks, the app should spawn a second thread and let the second thread perform the task.

Source code in Anr.zip

  1. MainActivity.java
  2. activity_main.xml
  3. strings.xml
  4. AndroidManifest.xml.
  5. build.gradle (Module: app)

Run the project

[Read these directions before you try to do them.] Run the project and press the button once. As usual, the button will change color (or acquire a shadow) in response to your touch. But note that the button doesn’t goes back to its original color.

Press the button a second time, one or two seconds after the first press. Then wait to see what happens.

LogCat output

You can see the following output in the LogCat window by clicking on the 6: Android along the lower edge of Android Studio. Or you can use adb as in Text.

adb devices
List of devices attached
0088080744830RTG	device
192.168.57.101:5555	device
emulator-5554	device

adb -s 192.168.57.101:5555 logcat ActivityManager:E '*:S'

Output on the Genymotion emulator running Samsug Galaxy S5

The ANR dialog in the above screen shot appears. The second press triggers the following messages.

08-10 19:29:48.219      539-554/? E/ActivityManagerANR in edu.nyu.scps.anr (edu.nyu.scps.anr/.MainActivity)
    PID: 1851
    Reason: Input dispatching timed out (Waiting because the touched window has not finished processing the input events that were previously delivered to it.)
    Load: 0.0 / 0.01 / 0.05
    CPU usage from 4161ms to -2068ms ago:
    2% 539/system_server: 0.4% user + 1.6% kernel / faults: 160 minor
    0% 143/debuggerd: 0% user + 0% kernel / faults: 7204 minor
    0.4% 212/mediaserver: 0.1% user + 0.3% kernel
    0% 597/com.android.systemui: 0% user + 0% kernel / faults: 17 minor
    0.3% 704/com.android.phone: 0% user + 0.3% kernel / faults: 6 minor
    0% 1//init: 0% user + 0% kernel
    0% 13/kworker/1:0: 0% user + 0% kernel
    0% 84/sdcard: 0% user + 0% kernel / faults: 4 minor
    0.1% 86/adbd: 0% user + 0.1% kernel
    0% 133/healthd: 0% user + 0% kernel
    0.1% 210/surfaceflinger: 0% user + 0.1% kernel
    0.8% TOTAL: 0.4% user + 0.3% kernel
    CPU usage from 1559ms to 2066ms later:
    1.9% 539/system_server: 0% user + 1.9% kernel / faults: 1 minor
    1.9% 702/Binder_4: 0% user + 1.9% kernel
    0% TOTAL: 0% user + 0% kernel

Output on the Android emulator running Nexus 5 API 22

I had to press the button several times, but I eventually got the ANR dialog.

08-10 19:37:59.419    1222-1240/? E/ActivityManager﹕ ANR in edu.nyu.scps.anr (edu.nyu.scps.anr/.MainActivity)
    PID: 2037
    Reason: Input dispatching timed out (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: 2.  Wait queue head age: 5667.8ms.)
    Load: 0.46 / 0.29 / 0.11
    CPU usage from 3735ms to -2413ms ago:
    1.2% 1222/system_server: 0.5% user + 0.7% kernel / faults: 3513 minor 2 major
    0% 1358/com.android.phone: 0% user + 0% kernel / faults: 1682 minor
    0% 1305/com.android.systemui: 0% user + 0% kernel / faults: 1443 minor 1 major
    0% 591/mtdblock0: 0% user + 0% kernel
    0% 599/mtdblock1: 0% user + 0% kernel
    0% 2037/edu.nyu.scps.anr: 0% user + 0% kernel / faults: 910 minor 4 major
    69% TOTAL: 23% user + 45% kernel
    CPU usage from 1885ms to 2404ms later:
    3.8% 1222/system_server: 0% user + 3.8% kernel / faults: 2 minor
    3.8% 1231/FinalizerDaemon: 0% user + 3.8% kernel
    40% TOTAL: 0% user + 40% kernel

Output on the Amazon HD6 Fire

I had to press the button several times. I never got the dialog.

08-10 19:47:54.514      519-535/? E/ActivityManager﹕ ANR in edu.nyu.scps.anr (edu.nyu.scps.anr/.MainActivity)
    PID: 2639
    Reason: Input dispatching timed out (Waiting because the touched window has not finished processing the input events that were previously delivered to it.)
    Load: 12.82 / 12.53 / 12.24
    CPU usage from 4829ms to -3846ms ago:
    18% 519/system_server: 11% user + 7.3% kernel / faults: 1145 minor 10 major
    1.6% 44/ksmd: 0% user + 1.6% kernel
    1.6% 825/com.android.systemui: 1% user + 0.5% kernel / faults: 278 minor 5 major
    0.8% 135/hotplug: 0.1% user + 0.6% kernel
    0.3% 795/com.amazon.imp: 0.2% user + 0% kernel / faults: 157 minor
    0.2% 976/com.android.phone: 0.2% user + 0% kernel / faults: 317 minor 1 major
    0.8% 2639/edu.nyu.scps.anr: 0.3% user + 0.4% kernel / faults: 131 minor 2 major
    0.1% 1106/android.process.media: 0.1% user + 0% kernel / faults: 156 minor 1 major
    0.4% 90/mmcqd/0: 0% user + 0.4% kernel
    0.3% 80/irq/258-inv_irq: 0% user + 0.3% kernel
    0% 156/vitals_collection_agent: 0% user + 0% kernel / faults: 100 minor 1 major
    0.3% 953/com.amazon.client.metrics: 0.3% user + 0% kernel / faults: 212 minor
    0.2% 66/rdma0_update_kt: 0% user + 0.2% kernel
    0.2% 89/disp_config_upd: 0% user + 0.2% kernel
    0.2% 326/adbd: 0% user + 0.2% kernel
    0.2% 1120/com.amazon.kindle.otter: 0.2% user + 0% kernel
    0.2% 31986/kworker/0:3: 0% user + 0.2% kernel
    0% 1//init: 0% user + 0% kernel / faults: 46 minor
    0.1% 7/migration/0: 0% user + 0.1% kernel
    0.1% 8/rcu_preempt: 0% user + 0.1% kernel
    0.1% 11/migration/1: 0% user + 0.1% kernel
    0% 15/migration/2: 0% user + 0% kernel
    0% 18/kworker/2:0H: 0% user + 0% kernel
    0.1% 19/migration/3: 0% user + 0.1% kernel
    0.1% 40/cfinteractivepl: 0% user + 0.1% kernel
    0.1% 76/irq/261-main_tt: 0% user + 0.1% kernel
    0.1% 96/bat_thread_kthr: 0% user + 0.1% kernel
    0.1% 125/jbd2/mmcblk0p14: 0% user + 0.1% kernel
    0.1% 149/surfaceflinger: 0.1% user + 0% kernel / faults: 3 minor
    0.1% 1033/com.amazon.device.logmanager: 0% user + 0.1% kernel / faults: 7 minor
    0.1% 1234/com.amazon.kindle.cms: 0.1% user + 0% kernel
    0.1% 2160/com.android.defcontainer: 0.1% user + 0% kernel / faults: 3 minor
    0% 27367/ksdioirqd/mmc1: 0% user + 0% kernel
    5.6% TOTAL: 0.7% user + 0.5% kernel + 4.4% iowait
    CPU usage of : edu.nyu.scps.anr = 7 , total = 869
    CPU Frequencies:					364000	390000	442000	507000	585000	637000	780000	806000	884000	975000	988000	1092000	1118000	1209000	1261000	1508000
    cpu0 cortex-a7 duration on (ms):	140		0		2		0		2		0		11		0		0		0		2		0		0		217		0		0
    cpu2 cortex-a15 duration on (ms):	0		64		0		6		0		8		0		0		0		0		0		0		0		0		0		62
    CPU usage from 2933ms to 3452ms later:
    15% 519/system_server: 9.4% user + 5.6% kernel / faults: 11 minor
    5.6% 535/ActivityManager: 1.8% user + 3.7% kernel
    5.6% 583/InputDispatcher: 5.6% user + 0% kernel
    1.8% 532/SensorService: 0% user + 1.8% kernel
    1.8% 540/android.ui: 1.8% user + 0% kernel
    1.8% 577/PowerManagerSer: 1.8% user + 0% kernel
    1.8% 1379/Binder_15: 1.8% user + 0% kernel
    3.8% 44/ksmd: 0% user + 3.8% kernel
    3.7% 2639/edu.nyu.scps.anr: 3.7% user + 0% kernel / faults: 44 minor
    3.7% 2639/du.nyu.scps.anr: 1.8% user + 1.8% kernel
    0.9% 149/surfaceflinger: 0.9% user + 0% kernel
    0.9% 423/HWC_vsync: 0.9% user + 0% kernel
    0.9% 326/adbd: 0% user + 0.9% kernel
    0.9% 326/adbd: 0% user + 0.9% kernel
    0.9% 825/com.android.systemui: 0% user + 0.9% kernel / faults: 1 minor
    0.9% 825/ndroid.systemui: 0.9% user + 0% kernel
    0.9% 831/Compiler: 0.9% user + 0% kernel
    1.4% 31986/kworker/0:3: 0% user + 1.4% kernel
    8.5% TOTAL: 3.2% user + 5.2% kernel
    CPU usage of : edu.nyu.scps.anr = 2 , total = 53

Things to try

  1. Print the number of the process, and the name and number of the thread within the process, that is executing the onClick method. Insert the following code at the start of the method. We saw the code in Text.
                    Thread currentThread = Thread.currentThread();
    
                    Log.d("myTag",
                        "PID = " + android.os.Process.myPid() + "\n"
                        + "thread name = " + currentThread.getName() + "\n"
                        + "thread ID = " + currentThread.getId());
    
    08-10 20:16:00.787    2140-2140/? D/myTag﹕ PID = 2140
        thread name = main
        thread ID = 1
    

    List the process and the threads within it:

    adb -s 192.168.57.101:5555 shell ps -p 2140
    USER     PID   PPID  VSIZE  RSS   PRIO  NICE  RTPRI SCHED   WCHAN    PC         NAME
    u0_a70    2140  211   593796 38956 20    0     0     0     ffffffff b766f07b S edu.nyu.scps.anr
    
    adb -s 192.168.57.101:5555 shell ps -p 2140 -t
    USER     PID   PPID  VSIZE  RSS   PRIO  NICE  RTPRI SCHED   WCHAN    PC         NAME
    u0_a70    2140  211   593796 38956 20    0     0     0     ffffffff b766f07b S edu.nyu.scps.anr
    u0_a70    2142  2140  593796 38956 20    0     0     0     c0183a45 b766f399 S GC
    u0_a70    2143  2140  593796 38956 20    0     0     0     c014bfe7 b766e7cb S Signal Catcher
    u0_a70    2144  2140  593796 38956 20    0     0     0     c06c29bd b766eb13 S JDWP
    u0_a70    2145  2140  593796 38956 20    0     0     0     c0183a45 b766f399 S Compiler
    u0_a70    2146  2140  593796 38956 20    0     0     0     c0183a45 b766f399 S ReferenceQueueD
    u0_a70    2147  2140  593796 38956 20    0     0     0     c0183a45 b766f399 S FinalizerDaemon
    u0_a70    2148  2140  593796 38956 20    0     0     0     c0183a45 b766f399 S FinalizerWatchd
    u0_a70    2151  2140  593796 38956 20    0     0     0     c05963ba b766d586 S Binder_1
    u0_a70    2152  2140  593796 38956 20    0     0     0     c05963ba b766d586 S Binder_2
    

    Another way to see the threads:
    Tools → Android → Android Device Monitor
    In the Devices tab in the left panel of Android Device Monitor, select your app edu.nyu.scps.anr. In the toolbar below the tab, click on the Update Threads icon. It has three parallel arrows pointing to the right. In the right panel, select the Threads tab. If some of the panels seems to be missing, click on the Maximize icon in the upper right.