I. Overview of ANR
ANR, or Application Not Responding, is an Application that does Not respond. If the Interface thread of an Android application is blocked for too long, an “app unresponsive” (ANR) error will be triggered. In this case, a dialog box is displayed. The ANR dialog box provides users with the option to forcibly exit the application.
Ii. Four types of ANR
On Android, the response of an application is monitored by two system services, Activity Manager and Window Manager. Generally, the system reports ANR when an application encounters the following four conditions:
- KeyDispatchTimeout (most common type) — ANR occurs due to input events not being processed within 5s, mainly keystrokes and touch events;
Keyword: InputDispatching Timeout
- BroadcastTimeout: — ANR occurs when the BroadcastReceiver does not complete processing within a specified period of time. Background broadcast 60s);
Keyword: Timeout of Broadcast BroadcastRecord
- ServiceTimeout — ANR occurs when the Service does not complete processing within a specific period of time. (Limit: Front desk service 20s; Background service 200s);
Log keyword: Timeout Executing Service
- ContentProviderTimeout — content provider, ANR occurs when processing is not completed within 10s;
Keyword: Timeout Publishing Content providers
Iii. Causes of ANR
- The main thread is blocked by IO operations (network IO is not allowed in the main thread after 4.0)
Error operations on the main Thread, such as Thread.wait or thread. sleep, will be monitored by the Android system and result in timeout for the main Thread. If the following two conditions occur, the ANR dialog box will be displayed.
- There are time-consuming calculations in the main thread:
For example, a large number of database reads and writes, time-consuming network conditions, high intensity of hardware computing, etc.
-
The BroadcastReceiver does not complete processing within 10 seconds
-
The Service could not process for 20 seconds at a certain time
Iv. How to avoid ANR:
-
Use AsyncTask to process time-consuming I/O operations.
-
Using Thread or HandlerThread, call * * Process in setThreadPriority (Process. THREAD_PRIORITY_BACKGROUND) * * set priority, otherwise, still can reduce response program, Because the default Thread has the same priority as the main Thread.
-
Instead of blocking the main Thread with ** thread.wait () or thread.sleep ()**, use handlers to process worker Thread results.
-
Try to avoid time-consuming code in the Activity’s onCreate and onResume callbacks. IntentService is recommended for onReceive code in BroadcastReceiver to minimize time consumption.
Put all time-consuming operations such as network access, Socket communication, querying a large number of SQL statements, complex logic calculations, etc. into the child thread, and then update the UI through handler.sendMessage, runonUIThread, AsyncTask, RxJava, etc.
V. How to solve the ANR situation:
If ANR still occurs when it has been avoided as far as possible, we can locate and analyze the problem according to the following steps:
1. Problem analysis
-
Export ANR logs and determine the package name, class name, process number, occurrence time, and cause type of ANR based on the logs.
-
The system resource information includes the CPU, memory, and I/O usage before and after ANR occurs.
-
Check the status of the main thread, check whether the main thread has problems such as time consumption, deadlock, and equal lock, and determine whether the ANR is caused by App or system.
-
Based on application logs, codes, or source codes, analyze whether the application is abnormal before ANR problems occur.
2. Export ANR logs
When ANR problem occurs, the system collects ANR logs, CPU usage, and trace logs, and generates a testament. TXT file in the **/data/ ANR /** path.
Note: Each time a new ANR problem occurs, the previous ANR information is overwritten.
You can export the local traces files using the ADB command.
adb root
adb shell ls /data/anr
adb pull /data/anr/<filename>
Copy the code
- None Example Read key log information
-
Find ANR generation information in the log: Traces file for keywords such as:
09-24 15:20:20.211 1001 1543 1570 XXXXXXX: ANR in XXXXXX 09-24 15:20:20.211 1001 1543 1570 XXXXXXX: PID: XXXXX 09-24 15:20:20.211 1001 1543 1570 XXXXXXX: Reason: XXXXXX
Among them:
- ANR in, including the package name and class name that cause ANR
- Where, is the PID of the process where ANR occurs
- Reason indicates the cause of ANR, for example, keyDispatchingTimedOut
-
Find CPU Usage information
09-24 15:20:20.211 1001 1543 1570 XXXXXX: CPUusage from XXX to XXX ago XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX 09-24 15:20:20.211 1001 1543 1570 XXXXXX: CPUusage from xxx to xxx later xxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxx
Among them:
- Ago represents CPU usage before ANR occurs
- Later indicates CPU usage after ANR occurs
- Focus on XXX %TOTAL: XXX % user + XXX % kernel + XXX % iowait to learn about CPU usage.