Android ANR problem summary

1.traces file analysis

ANR stack information is output to the file /data/anr/traces.txt

2. Three common anr problems

a. The main thread is blocked and there is no response for 5s of input events, such as onClick event.

b.BroadcastReceiver is running in the main thread of the program, and the runtime of BroadcastReceiver is 10s by default.

c.Service is a computing component. Although it runs in the background, it essentially runs on the main thread. If your service needs to do any CPU-intensive (such as MP3 playback) or blocking (such as network) operations, you must put In the child thread, otherwise it takes more than 20s to cause anr.

3. Other reasons

a. CPU is intensive, which makes the main thread unable to seize CPU time slices, pay attention to the process with high CPU usage
b. High IO, such as improper access to the database leads to excessive database load (the use of iowait in the CPU is high in the log)
c. Low memory (low memory), such as insufficient memory causes the block to create a bitmap
. A deadlock triggers ANR, and the non-main thread holds the lock object required by the main thread, causing the main thread to wait for a timeout. Usually the log will have the following fields Blocked | -locked | waiting to lock | held by thread, at this time most of the CPU is idle, and the usage rate is very low
. e. The current application process performs inter-process communication to request other processes. The operation of other processes has no feedback for a long time, such as operating the hardware Camera
f .Service binder number reaches the upper limit
g. Trigger WatchDog ANR in system_server

4. How to avoid ANR

a. Never perform complex and time-consuming operations on the main thread, such as sending and receiving network data, performing large amounts of calculations, operating databases, reading and writing files, etc., all using asynchronous operations.
b.broadCastReceiver To perform complex operations, you can start an IntentService or JobIntentService in the onReceive () method to do it.
The time-consuming operation in c.Service is also best to use asynchronous tasks.
d. Avoid inappropriate situations such as synchronization / deadlock and dead loop in the design and code writing stage.

5. Log analysis

关键词:Blocked | - locked | waiting to lock | held by thread  |ANR in |ANRManager: Reason

Published 142 original articles · praised 258 · 160,000 views

Guess you like

Origin blog.csdn.net/conconbenben/article/details/105329095