ANR

concept

ANR means the application is not responding. If the UI thread is blocked for too long, an ANR will appear, and the box will pop up, terminating the process and continuing to wait.

type

KeyDispatchTimeout (common) Input event not completed within 5S ANR occurred. Logcat Keyword: Input event dispatching timed out

BroadcastTimeout Foreground Broadcast: ANR occurs when onReceiver does not complete processing within 10S. Background Broadcast: ANR does not occur on onReceiver within 60 seconds. Logcat Log keyword: Timeout of broadcast BroadcastRecord

ServiceTimeout Foreground Service: ANR occurs if the life cycle of onCreate, onStart, and onBind is not completed within 20 seconds. Background Service: onCreate, onStart, onBind: Life cycle is not complete within 200s ANR Logcat keyword: Timeout Executing Service

ContentProviderTimeout An ANR occurs without processing completion of the ContentProvider within 10S. Logcat Log keyword: timeout publishing Content providers

Conclusion:

The reasons causing

1: The main thread performs time-consuming operations, such as database read and write, IO read and write, network request, etc. 2: Deadlock of multi-threaded operations, the main thread is blocked; 3: The main thread is blocked by Binder peer. 4: ANR appears in the WatchDog in the System Server. 5: The service binder is connected online and cannot communicate with the System Server. 6: System resources (pipes, CPUS, and IO) are exhausted.

To solve

1. Do as little work as possible in the main thread and place time-consuming operations in child threads. Especially in the Acitivty lifecycle (onCreate/onResume()) to reduce the creation of potentially time-consuming operations. (Network requests, database operations, bitmap size calculations, etc.) are placed in child threads and then submitted to the main thread via Handler instead of waiting (), sleep() and so on

2. Stop doing time-consuming operations on the radio. Put it in a Service or a child thread.

3. If there is a time-consuming operation, create a progress bar to prevent users from thinking that the machine has crashed.

Capture and monitoring

1. ANR appears during the development phase

In the development phase is still relatively easy to solve, just need to read the error of the log. Then locate the specific number of lines of code. If not, go to the /data/anr/traces. Tet file, which keeps the ANR log.

1.CPU, user mode, core mode, IO usage. Which one is being used more, that one is in trouble. 2. Stack utilization. 3. Look at the error situation of “main”, my own error is usually here.

1. Integrate third-party monitoring: Bugly, Dandelion, aurora, etc., and watch the phone model and error log of ANR. FileObserver is an abstract class, define a subclass to inherit it, implement onEvent method. Then monitor /data/anr/ this directory to see if there is a new file, if there is anR, and then upload to the server. This is what the Android system uses

Crash monitoring

“Crash” means to stop working quickly. Flash retreat is divided into Java layer flash retreat and Native layer flash retreat.

The Crash of the Java layer

A flash exit is when an exception occurs that is not caught and can’t be handled, then a box pops up that says “Program crashes” and the app exits.

1. Obtain UncaughtExceptionHandler Thread. GetDefaultUncaughtExceptionHandler (); 2. Implement interface UncaughtExceptionHandler 3. In this method we have uncaughtException

The Throwable can be saved locally and then sent to the server (write your own time, phone model, app version, etc.).

Then, most importantly, call the system method uncaughtException, otherwise the program won’t crash, but it will freeze and restart indefinitely.

What did the system do

RuntimeInit.java -> KillApplicationHandler

Also implements the UncaughtExceptionHandler interface

1. Pass the AMS popover in try

2. Kill the app process in finally