[Huawei Developers] Freemind R:
1. Introduction to ANR

1.1 What Is ANR?

ANR is short for Application Not Responding. Such problems occur when the UI thread of an Android app is blocked for a long time.

In this case, a pop-up is displayed, providing an option for users to exit the app by force.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028093129.24604724200508065530203631996154:50511109060513:2800:B0117BD96BC50AE11EDD1A98840089DDF73CE86BC3BA3BB40BDEDF5B17DCC2B3.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028093129.24604724200508065530203631996154:50511109060513:2800:B0117BD96BC50AE11EDD1A98840089DDF73CE86BC3BA3BB40BDEDF5B17DCC2B3.jpg[/IMG]

1.2 What Are the Types of ANR Problems?

Apps running on Android devices are monitored by the Activity Manager and Window Manager system services. Generally, the services report ANR problems in the following cases:

KeyDispatchTimeout (the most common type): Triggered when an input event fails to be processed within 5s, including key-pressing events and touch events.

Log keyword: InputDispatching Timeout

BroadcastTimeout: Triggered when BroadcastReceiver fails to respond within a specific period (10s for foreground broadcasts and 60s for background broadcasts).

Log keyword: Timeout of broadcast BroadcastRecord

ServiceTimeout: Triggered when Service fails to respond within a specific period (20s for foreground services and 200s for background services).

Log keyword: Timeout executing service

ContentProviderTimeout: Triggered when ContentProvider fails to respond within 10s.

Log keyword: Timeout publishing content providers

1.3 Why Do ANR Problems Occur?

We have concluded the following typical ANR scenarios based on massive ANR case analysis:

The main thread is locked by another thread (proportion: 57%). The sleep() and wait() methods of the thread are called, causing the waiting timeout of the main thread.

The system resource is occupied (proportion: 14%). An app fails to obtain sufficient system resources because other processes occupy a large number of system resources (CPU, RAM, and I/O).

The main thread is suspended due to time-consuming tasks (proportion: 9%). The main thread is suspended due to a large number of database reads and writes, unstable network conditions, and high-intensity hardware computing.

2. Methodology for Solving ANR Problems

2.1 Overall Approach

1. Export ANR log information and check the name of the package or class in which ANR occurs, involved process ID, occurrence time, and cause type.

2. Check the system resource information, including the usage of system resources such as CPU, RAM, and I/O before and after ANR occurs.

3. Check the status of the main thread for any fault such as time-consuming operation and deadlock, and determine whether the ANR problem is caused by the app or OS.

4. Check whether the app is abnormal before the ANR problem occurs based on app logs or code.

2.2 Exporting ANR Logs

When an ANR problem occurs, the system collects ANR-related log information, CPU usage, and trace logs (how each thread is executed), generates a traces.txt file, and saves the file in the /data/anr/ directory.

Note: Each time a new ANR problem occurs, the previous ANR information is overwritten.

You can run the adb command to export the trace file to your computer.

adb root
adb shell ls /data/anr
adb pull /data/anr/<filename>

2.3 Reading Key Log Information

1. In the log information, locate information by specific keyword.

Keyword examples in the trace file:

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

The details are described as follows:

ANR in: Name of the package or class in which ANR occurs

PID: ID of the process in which ANR occurs

Reason: Cause of ANR such as keyDispatchingTimedOut

2. Check the 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

The details are described as follows:

ago: CPU usage before ANR occurs

later: CPU usage after ANR occurs

Note: Pay attention to TOTAL, user, kernel, and iowait to check the CPU usage.

2.4 Analysis

After analyzing the CPU usage, if the cause is still not clear, you need to further analyze the trace file. The file records each thread stack of the involved process before and after ANR occurs. Analyze the main thread stack information to check whether the app is abnormal before ANR occurs.

ANR problems vary depending on the scenario. Therefore, the solution here is provided for reference only.

3. ANR Troubleshooting

3.1 Difficulties

In most cases, users require proactive app response to their operations, such as button tapping, resource loading, and page redirection. If an ANR problem occurs, merely a pop-up indicating that the app does not respond is not enough to meet user requirements.

However, it is difficult to deal with ANR problems for many reasons:

1. ANR problems often occur on out-of-date devices or in poor network conditions, that tests can hardly cover.

2. Some ANR problems occur randomly, which are difficult to locate. An actual device that can be used to reproduce the problem is required for detailed log information, but not all devices can come in handy.

3. ANR problem locating is complex and affected by many factors, demanding much experience and expertise.

3.2 New Solution

In addition to traditional methods, third-party app monitoring platforms can also be introduced to handle ANR problems.

To locate and solve ANR problems and improve user experience, more and more service providers start to develop app performance monitoring tools.

HUAWEI AppGallery Connect is one of them. It provides App Performance Management (APM) with minute-level app performance monitoring capabilities such as ANR analysis. With ANR monitoring of AppGallery Connect APM, you can:

1. Monitor ANR problems on the live network in real time and master their trends.

2. Collect and view ANR information online without reproducing the problems.

3. Locate and solve ANR problems quickly and systematically.

4. ANR Solving Cases

The following demonstrates how to locate typical ANR problems using the APM service with some cases.

4.1 Case 1: ANR Caused by a Deadlock

4.1.1 Finding the Problem

Sign in to AppGallery Connect, click My projects, find your project, and click your app on the project card. Then go to Quality > APM > ANR analysis. It is found that the ANR-affected user rate of the top one problem type reaches 16.67%. This type of problems needs to be solved by priority.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028093220.35993340099625601089401604120554:50511109060513:2800:05105DA43F4AF624919810C6F600F1D69A43A8EF44EC1A7717B1CFCC63FBD9C9.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028093220.35993340099625601089401604120554:50511109060513:2800:05105DA43F4AF624919810C6F600F1D69A43A8EF44EC1A7717B1CFCC63FBD9C9.jpg[/IMG]

4.1.2 Locating the Problem

Click the card of this problem type among top problems. The ANR details page of this type of problems is displayed.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112122.40077467943089963748053406973054:50511109060513:2800:88047942E099DE540D7A2EFB7A5DB780B418285948E7656FE2AB436DC4FB98E8.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112122.40077467943089963748053406973054:50511109060513:2800:88047942E099DE540D7A2EFB7A5DB780B418285948E7656FE2AB436DC4FB98E8.jpg[/IMG]

According to the user distribution pie chart, the ANR problem affects the most users when the app version is 2.0, the device model is HUAWEI VOG-AL10, or the OS version is 10.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112147.43998602611336449169522354809870:50511109060513:2800:CACEBFC608BB144B7D4A25052F25E3DF4961D954083831698E8837151CC33F8A.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112147.43998602611336449169522354809870:50511109060513:2800:CACEBFC608BB144B7D4A25052F25E3DF4961D954083831698E8837151CC33F8A.jpg[/IMG]

In the Records table under the charts, find a record that meets the preceding conditions, and click View details.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112201.42984783815078449010341726114326:50511109060513:2800:3E3E040C95AF73B8287ED0FD00871F40036224AACC66FB007BFA34DFE5660275.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112201.42984783815078449010341726114326:50511109060513:2800:3E3E040C95AF73B8287ED0FD00871F40036224AACC66FB007BFA34DFE5660275.jpg[/IMG]

4.1.2.1 Analyzing System Resource Status

According to the report, when the problem occurs, the CPU usage is 20%, the I/O usage is 0%, low memory does not occur, the allocated heap size is 26.50 MB, the used heap size is 8.69 MB, and the number of threads is 61. Such information indicates that system resource usage is normal.

ANR problems can be caused by insufficient system resources or incorrect code logic. Based on the preceding system resource information, this type of ANR problems is not caused by insufficient system resources, and there is only one possible cause, that is incorrect code logic. So the problem analysis needs to focus on code logic verification.

4.1.2.2 Checking the Main Thread Status to Find the Code Snippet That Causes ANR

For ANR problems caused by code logic, check the main thread stack and the thread status. On the Main thread stack info tab page, find the faulty stack. It is found that the main thread is obtaining the lock status when the problem occurs. Therefore, it can be concluded that the problem occurs because the main thread keeps waiting for the lock resource and is therefore blocked. As a result, subsequent input events are not responded, and an ANR problem of the Input dispatching timed out type is triggered.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112253.96105889321233217428635254985414:50511109060513:2800:7C08FEDA96CB8FB81EFB12A420AC851A690BFBDA8E45EBDD9FFC29FD5C868A5C.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112253.96105889321233217428635254985414:50511109060513:2800:7C08FEDA96CB8FB81EFB12A420AC851A690BFBDA8E45EBDD9FFC29FD5C868A5C.jpg[/IMG]

Locate the code snippet of the ANR problem in stack information. It is found that the deadlock occurs during the call of com.aiops.hiperformance.MainActivity.dispatchActivityDestroyed. Check the code. It is found that the deadlock occurs in the mLock.readLock().lock() function.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112309.53412959751780528883627690732702:50511109060513:2800:126967A0B07C8FFBD8BF6AC02F949A902A89D870F9F196F9902F7C3B827F616E.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112309.53412959751780528883627690732702:50511109060513:2800:126967A0B07C8FFBD8BF6AC02F949A902A89D870F9F196F9902F7C3B827F616E.jpg[/IMG]

Search for the keyword mLock in the code. It is found that only the MainActivity file contains the mLock.readLock.lock() code. Therefore, it can be determined that the abnormal code exists only in the MainActivity file, and we can narrow down the fault scope. During coding, if a lock is not released, an unexpected exception may have already occurred. This may lead to lock release failures. Therefore, you need to check whether the app is abnormal before the problem occurs.

Find the start time of the lock application action, search for any exception that occurs earlier than the deadlock occurrence. Switch to the ANR info tab page. It is found that the first element of the primary execution queue already exists before 5.5s. The ANR occurrence time is 2020-09-27 09:48:27. Therefore, the lock obtaining action is performed at about 2020-09-27 09:48:21.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112327.41751183623277248152750484370813:50511109060513:2800:14E5CFBFE0FEF081F1DE1A7252CA57AF6DE59B8871A32FB0570561F790C2ABDD.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112327.41751183623277248152750484370813:50511109060513:2800:14E5CFBFE0FEF081F1DE1A7252CA57AF6DE59B8871A32FB0570561F790C2ABDD.jpg[/IMG]

4.1.2.3 Viewing App Logs

Switch to the System logs tab page. Now we know that the lock obtaining action occurs at about 2020-09-27 09:48:21. Analyze logs generated before the time point for any exception that may lead to the failure to release the lock.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112344.77485631841101689443083187332254:50511109060513:2800:FBB3045036A45B07055FF5D45CCC50C841F446C67A8381E1E3FFDD99FB2F868E.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112344.77485631841101689443083187332254:50511109060513:2800:FBB3045036A45B07055FF5D45CCC50C841F446C67A8381E1E3FFDD99FB2F868E.jpg[/IMG]

It is found that the system throws the OutofBoundsException exception at 09:48:18.365 and generates abnormal stack information. The exception indeed occurs in the MainActivity file, that is, the code scope we have narrowed down to. According to the stack information, the abnormal code is finally located.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112406.38655800341614513125528817592499:50511109060513:2800:711758C151032590DA0DB6E405D45D6253D132B4205FC5C7F8C9CC247018E70E.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112406.38655800341614513125528817592499:50511109060513:2800:711758C151032590DA0DB6E405D45D6253D132B4205FC5C7F8C9CC247018E70E.jpg[/IMG]

It is found that an out-of-bounds error occurs when getShareDataInterceptor is called. As a result, mLock.readLock is not released. By now, we have completely found the cause: The lock resource fails to be released due to an exception, leading to the deadlock of the main thread.

4.1.3 Solving the Problem

To solve the problem and prevent similar problems, the following measures need to be taken:

1. Analyze the cause of the exception and modify the code to prevent out-of-bound exceptions

2. Capture similar exceptions and execute the protection code to throw the exceptions before the lock resource is released.

3. Check other code and add a protection mechanism before the lock resource release to ensure that the lock resource can be released in a timely manner.

4.2 Case 2: ANR Caused by Insufficient I/O Resources

4.2.1 Locating the Problem

Directly go to the details page of the ANR to utilize APM to locate the specific problem.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112431.75906083962560005851214789120805:50511109060513:2800:1B1A9853301CEF0579D26F14FB6A205C73AA4FABE4AA2A1DE7A6D0B3B4E7879E.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112431.75906083962560005851214789120805:50511109060513:2800:1B1A9853301CEF0579D26F14FB6A205C73AA4FABE4AA2A1DE7A6D0B3B4E7879E.jpg[/IMG]

According to the experience of locating most ANR problems, this ANR problem is caused by insufficient system resources. Therefore, the troubleshooting approach should be as follows: Locate the code that causes the ANR problem, and optimize the code so that ANR will not be triggered even if the I/O usage is high.

4.2.1.2 Finding the Cause by Checking the Main Thread Status

Switch to the Main thread stack info tab page and check the main thread code.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112729.94895623700956646745394596359794:50511109060513:2800:DFFED95DB6C37CDC0F7E13FB79DABE80D1D5E898E52A3439402B54CB9781D096.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112729.94895623700956646745394596359794:50511109060513:2800:DFFED95DB6C37CDC0F7E13FB79DABE80D1D5E898E52A3439402B54CB9781D096.jpg[/IMG]

According to the main thread stack information, the main thread directly performs database operations. When the system I/O usage is high, these operations will cause main thread blocking. Locate the involved code snippet based on the stack information.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112750.36072475069862988793403774390199:50511109060513:2800:65C3D5526CF1F678F0C917C9B560FF88AD25FC28E85BEB526CA8E8366126A542.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112750.36072475069862988793403774390199:50511109060513:2800:65C3D5526CF1F678F0C917C9B560FF88AD25FC28E85BEB526CA8E8366126A542.jpg[/IMG]

Therefore, it is confirmed that the code contains the operation of accessing the SQLite. Experienced developers will know that the problem can be solved merely by perform I/O operations in a thread.

4.1.2.3 Viewing App Logs

This step is not necessary as we have already located the cause of ANR.

4.2.2 Solving the Problem

Optimize the code as follows to prevent such ANR problems.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112822.55672647290717672260628673239660:50511109060513:2800:EE22E429F1B419BFB32A2313D71A40CA7A2013EA26B40E4374F48191DD376962.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112822.55672647290717672260628673239660:50511109060513:2800:EE22E429F1B419BFB32A2313D71A40CA7A2013EA26B40E4374F48191DD376962.jpg[/IMG]

4.3 Case 3: ANR Caused by the Main Thread Infinite Loop

4.3.1 Locating the Problem

Go to the details page of the ANR to utilize APM to locate the specific problem.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112902.16728448280874652516734179431026:50511109060513:2800:8EA3E6CA45C5FA59E7CAA0B26F9B510D60DB3D09115806EC3BF59600A33B3243.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112902.16728448280874652516734179431026:50511109060513:2800:8EA3E6CA45C5FA59E7CAA0B26F9B510D60DB3D09115806EC3BF59600A33B3243.jpg[/IMG]

4.3.1.1 Analyzing System Resource Status

According to the report, when the problem occurs, the CPU usage is 25%, the I/O usage is 0%, low memory does not occur, the allocated heap size is 18.01 MB, the used heap size is 8.08 MB, and the number of threads is 43. Such information indicates that system resource usage is normal.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112925.98650128860822258344267076189682:50511109060513:2800:E8154489DD884D46C3ED62D4E0E6C3F6573CD713AFC9C57C44C9F9981DCE3D33.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112925.98650128860822258344267076189682:50511109060513:2800:E8154489DD884D46C3ED62D4E0E6C3F6573CD713AFC9C57C44C9F9981DCE3D33.jpg[/IMG]

According to the experience of locating most ANR problems, this type of ANR problems is not caused by insufficient system resources, and there is only one possible cause, that is incorrect code logic. So the problem analysis needs to focus on code logic verification.

4.3.1.2 Finding the Cause by Checking the Main Thread Status

For ANR problems caused by code logic, check the main thread stack and the thread status. On the Main thread stack info tab page, find the faulty stack.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112943.57937982504064304241113100576076:50511109060513:2800:F0586FC6F93E54ECFC16A88EFC36E6B1FF04491F1243A5C0B68D5E37FAD96EAC.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112943.57937982504064304241113100576076:50511109060513:2800:F0586FC6F93E54ECFC16A88EFC36E6B1FF04491F1243A5C0B68D5E37FAD96EAC.jpg[/IMG]

It is found that the main thread stack is blocked in getActivity and is in the SUSPENDED status. According to the stack information, the abnormal code is finally located.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112957.80658705320206818296172848183328:50511109060513:2800:8854C61BBE3CE5B5D8D89E1BDA9C1B618DF1B64E3AB274D29B50FBDAC6CE4DFB.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028112957.80658705320206818296172848183328:50511109060513:2800:8854C61BBE3CE5B5D8D89E1BDA9C1B618DF1B64E3AB274D29B50FBDAC6CE4DFB.jpg[/IMG]

After the analysis, it is suspected that an infinite loop occurs on the main thread. If an infinite loop occurs in an app, the CPU user-mode time of the app increases abnormally. Switch to the ANR info tab page and view the CPU usage information of each process.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028113011.69930944514677923918157945335219:50511109060513:2800:6B2E62207DD3571976F3BFA8808718C938C0C27E26508E7B8FD887D31A4FFF35.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028113011.69930944514677923918157945335219:50511109060513:2800:6B2E62207DD3571976F3BFA8808718C938C0C27E26508E7B8FD887D31A4FFF35.jpg[/IMG]

It is found that the CPU usage of the app in user mode reaches 94%. Therefore, it is verified that an infinite loop occurs in the main thread, which causes the ANR problem.

4.3.1.3 Viewing App Logs

This step is not necessary as we have already located the cause of ANR.

4.3.2 Solving the Problem

Optimize the code as follows to prevent such ANR problems.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028113032.83894003751996977470816785973688:50511109060513:2800:CAB37E5B7231EEEF9910E45D83264E7D4F479A1E2F1376AB1378A9A10C7A4E77.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201028113032.83894003751996977470816785973688:50511109060513:2800:CAB37E5B7231EEEF9910E45D83264E7D4F479A1E2F1376AB1378A9A10C7A4E77.jpg[/IMG]

5. Case Summary

The preceding ANR problems are resolved with the help of HUAWEI AppGallery Connect APM, especially based on the ANR analysis reports and ANR records provided by the service.

You can view the ANR trend information in different dimensions such as app version, device model, OS version on the ANR analysis page of AppGallery Connect APM. You can analyze the trend and trigger condition of a certain type of ANR problems.

In addition, you can obtain more detailed device information, OS information, app information, and stack logs when ANR occurs based on the detailed problem occurrence records, and quickly locate the problem.

6. References

APM official documentation