Learn with you for lifeXi, this is Programmer Android
Recommended classic articles. By reading this article, you will gain the following knowledge points:
1. Overview of ANR
2. Types of ANR
3. Causes of ANR
4. How to analyze and solve ANR problems
5. ANR problem analysis and solution suggestions
6. Analysis of MTK platform ANR problems
1. Overview of ANR
In Android
, application responses are monitored by Activity Manager
and system services. , is a self-protection measure. When the main thread is stuck, the system will give the user a pop-up prompt, allowing the user to manually choose to continue waiting or forcefully close the thread .Window Manager
ANR(Application Not Responding )
Android
Android
APP
When Android detects one of the following situations, a dialog box Android
for a specific application will be displayed ANR
. The following three situations ANR
will often occur:
1.
UI Thread
More than5 s
no response2. No response
Broadcast
after broadcast exceeds10 s
3.
Service
The service exceeds the limit20s
and does not respond.
Therefore, to avoid ANR
this happening, please do not perform time-consuming operations in the main thread. Please run time-consuming operations in child threads as much as possible.
4.
ANR
The screenshot of the occurrence is as follows:
ANR Dialog example
2. Types of ANR
ANR
It is very common in Android
mobile phones and can be divided into the following three common types according to their corresponding types.
ANR types are as follows:
The key response distribution timeout (Key Dispatch Timeout)
is by default5 s
. If exceeded, ANR will occur.The broadcast timeout (Broadcast Timeout)
is by default10 s
, if exceeded, ANR will occur.The service timeout (Service Timeout)
is the default20 s
. If it exceeds, ANR will occur.
3. Causes of ANR
In Android
the system, APP
it usually runs in a UI Thread
or called MainThread
. And Android
there is only one MainThread
sum in Main Message Queue
. MainThread
Mainly used UI
for drawing, event response, monitoring and receiving event processing and other functions. Main Message Queue
A queue that mainly stores messages that users want to process. After the main thread gets the message MainThread
from the message queue , it distributes it as soon as possible. Once a certain message distribution times out, it may happen.Main Message Queue
Message
ANR
Therefore, when ANR
it occurs, we need to analyze ANR
the cause, that is, find the reason why the message is not processed in time. For example, it can be analyzed from the following question points:
1. Why
APP
can’t I getCPU
the time slice?2.
APP
Are you waiting for some events that cannot be processed in time to be completed?3. Is the message processing process too complicated?
4. How to analyze and solve ANR problems
ANR
There are some common patterns to choose from when analyzing :
APP
A slowI/O
operation is taking place on the main thread.APP
Very complex calculation operations are being performed in the main thread.The main thread is performing a synchronous program call to another process
Binder
, but the other process is taking a long time to return the results.The main thread is blocked while waiting for another child thread that is performing a long block operation.
The main thread is deadlocked by another thread. Whether it is
Bind
a call or a main thread call, the main thread cannot wait for a long time, let alone perform complex calculations in the main thread.
Knowing the causes of ANR, how to avoid ANR problems?
1.Strict mode
Using StrictMode
can help you detect unexpected operations on the main thread while developing your application I / O
. You can use application
or .activity
StrictMode
2. Close the ANR Dialog prompt
View methods controlled by method ANR:
Settings----Developer Options---显示所有ANR
Note:
If there is no developer option, please go to settings---about phone---click the version number multiple times to open the hidden developer option item
Background app ANR switch
3.Traceview
Traceview obtains the tracing information of the running application. By analyzing this traces.txt
file, you can infer that the main thread is busy with something.
traces
The file is usually saved /data/anr/traces.txt
under , and you can adb cat
view it directly or adb pull
export it.
It is recommended to use this method
adb root
adb remount
adb pull /data/anr/traces.txt .
pull traces file to desktop
5. ANR problem analysis and solution suggestions
Analyze to see ANR
the cause and then solve ANR
the problem.
1. Time-consuming operation
Please place it at the work site and it can be used Handler、AsyncTask
etc.
2. IO operations
(For example: network operations, storage operations, etc.) are also common factors that cause ANR. It is highly recommended to do this in a worker thread.
3. Program lock competition
In some cases, ANR
the cause is not directly caused by the main thread. For example: the working thread 资源
locks a certain wait, and the main thread needs it at this time 资源
. If the wait times out, ANR may occur at this time.
4. Deadlock
ANR
This may occur when the main thread enters the wait state because it is requesting a resource that is being held by another thread .
5. Slow broadcast reception
Applications can respond to broadcast messages through broadcast receivers, such as enabling or disabling airplane mode or changing connection status. ANR
It can happen when an application takes too long to process a broadcast message, theoretically exceeding 10 seconds before processing is completed .
6. Broadcast ANR occurs under the following circumstances:
onReceive()
The method has not been executed for a long time.
Try to avoid onReceive()
time-consuming operations in .
The broadcast receiver called
goAsync()
a method and it failedPendingResult
to be called on the objectfinish()
.
If there is a lot of broadcast content to be processed, please use IntentService
.
For example:
3. It is not recommended to perform time-consuming operations in the onReceive method. If it is not processed for more than 10 seconds, it will cause ANR.
It is not recommended to perform time-consuming operations in the onReceive method. If it is not processed for more than 10 seconds, it will cause ANR.
Recommended to use
IntentService
to avoid ANR occurrence
IntentService avoids ANR caused by processing too many broadcast messages
Your broadcast receiver can be used goAsync()
to notify the system that more time is needed to process the message. However, you should PendingResult
call it on the object finish()
. The following example shows how to call finish() to let the system recycle the broadcast receiver and avoid ANR:
goAsync()---finish gets more broadcast response time
6. Analysis of ANR issues on MTK platform
Prerequisite, grab a ANR
copy MTK log
.
1.event_log
Search keywords am_anr
or anr
analyze and see ANR
the reasons
event_log analyzes the causes of ANR
2. main_log
Search for keywords Application Not Responding
or anr
analyze and see ANR
why.
Analyze ANR causes in main_log
3. MTK ANR strategy recommendations
MTK official summary chart
MTK ANR analysis steps
MTK ANR Debug SOP
MTK ANR Debug SOP
4. Common ANR examples are analyzed as follows:
Main Thread is idle
Stuck in IO
Main Thread Waiting a lock
Wait Binder Transaction
Main Thread Query DB
references:
[Tencent Documentation] Android Framework Knowledge Base
https://docs.qq.com/doc/DSXBmSG9VbEROUXF5
Friendly recommendation:
Collection of useful information on Android development
At this point, this article has ended. The editor thinks the article is reprinted from the Internet and is excellent. You are welcome to click to read the original article and support the original author. If there is any infringement, please contact the editor to delete it. Your suggestions and corrections are welcome. We look forward to your attention and thank you for reading, thank you!
Click to read the original article and like the boss!