I. Introduction
The ANR problem is a common and troublesome problem in Android. It is difficult to analyze the cause in many cases.
Ii. ANR Definition
Next let's take a lookBaidu encyclopediaDefinition for ANR:
ANR problems are often caused by complex and time-consuming operations in the main (main thread) thread, such as file IO, network access, and infinite loops. Finally, ANR is throttled by the system.
Iii. General ANR analysis ideas
1) Export the traces.txt file from the/data/traces/directory of the mobile phone;
2) Obtain ANR generation time point T1 from the traces.txt file;
3) obtain the running status and call stack of the main thread from the traces.txt file;
4) view the logcat log, search for "ANR in", and find the time point t2 generated by ANR, and the number of seconds before and after the time point t2;
5) in Step 4, find out the CPU time consumption ratio of the target process, and check whether the CPU usage is high, the kernel usage is high, or the iowait usage is high;
6) calibrate the time range [T1, T2] or [T2, T1] generated by ANR Based on Time Point T1 and time point t2;
7) view the logcat log and locate the log to step 2. The obtained time point is near [T1, T2] or [T2, T1;
8) Check the logs of logcat in the vicinity of [T1, T2] or [T2, T1] for two minutes to restore the scenarios before and after ANR is generated;
9) locate possible problematic code Blocks Based on the information obtained from the preceding steps;
10) solve the problem or propose a stage analysis conclusion.
Iv. ANR analysis example
After the ANR is generated, the system will generate the traces.txt file under/data/traces/. We will first export the traces.txt file from the corresponding directory on the mobile phone.
After traces.txt is opened, confirm the ANR generation process and ANR generation time. As shown in the red box, the ANR generation process is CN. Evergrande. It. Phone. the ANR generation time is10:34:43,
In anranalysis, the time point of an anr is very important. It is the connection line connecting traces.txt and logcat-related log clues. It is an important clue to the cause of ANR reasoning.
Continue the traces.txt analysis and find the main thread of the CN. Evergrande. It. Phone process to call the stack snapshot, as shown in.10:34:43It is in the runnable status and is
Run the Java-Layer Code to measure the recylerview in the layout.
View the logcat log, search for "ANR in", and locate the CPU usage from 35958 ms to 0 Ms ago log, as shown in.10:34:07. 546To10:34:43. 504During the time period, the CN. Evergrande. It. Phone process does not have a large CPU proportion.
Continue to view the logs of CPU usage from 372 ms to 893 ms later.10:34:43. 877To10:34:44. 398Time period, ANR problems begin to appear,
CN. evergrande. it. phone process grande. it. phone, HDLogicThread-2, reqtimer three threads CPU ratio is relatively high, it can be inferred that is CN. evergrande. it. the phone process triggers ANR.
Based on the above-mentioned ANR logs of ago and later, we can basically conclude that the source of ANR is the CN. Evergrande. It. Phone process,
Main thread ID is 20974, so it can be determined that
CN. Evergrande. It. Phone process blocking causes ANR.
Next, confirm the cause of ANR generation, first calibrate the ANR generation time point,
1) the time point given by traces.txt is10:34:43
2) the time range given by logcat is10:34:43. 877To10:34:44. 398
When the intersection of 1) and 2) time (point) is integrated, the time range of ANR generated after calibration is10:34:43. 877To10:34:44. 398.
Next, check that logcat10:34:43. 877To10:34:44. 398Range of logs to restore the situation when ANR is generated.
The log shows that logcat10:34:43. 877To10:34:44. 398Nearby logs can be used to restore the video that is being played by the system during this time period, and the user is operating the progress bar to control the playing of the music,
However, there is no line related to the original cause of anrworkflow. traces.txt is generated when the main thread is runnable. Therefore, the possible cause of ANR generation cannot be analyzed from the main call stack.
V. Summary
The example in Section 4 shows that there is not enough data to analyze the cause of ANR generation. However, this article aims to tell readers a new way of analyzing ANR for your reference and constant improvement.
ANR analysis ideas