Different ways of Analyzing ANR in Android


Using Bug Report:

Reproduce the ANR and Capture the Bug report via Take bug report option from Developer options or via adb bugreport command. Search for Reason for ANR or 

am_anr in the Bug report to find which component it’s pointing to.Analyse the logs  just 5 seconds or 10 seconds before this based on the component it’s referring to , to find the root cause.

Using traces file:

After ANR is recreated pull the traces file via adb pull /data/anr/traces.txt command.

Traces file consists of the states of threads.There are different states of threads such as Waiting,Sleeping, Native, Suspended etc. Mainly focus on Suspended thread and find the reason for suspension. Also try to find the Deadlocks by searching for waiting to lock

An example of Deadlock is mentioned below 

DALVIK THREADS (16):

“main” prio=5 tid=1 Blocked

  ….

  – waiting to lock <0x3a357895> (a android.os.MessageQueue) held by thread 12

  at android.os.Looper.loop(Looper.java:122)

  ….

  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

  “SampleHandlerThread” prio=5 tid=12 Blocked

   …..

  at android.os.MessageQueue.next(MessageQueue.java:145)

  – waiting to lock <0x32085e66> (a android.os.MessageQueue) held by thread 1

  at android.os.Looper.loop(Looper.java:122)

In the above example the main thread is held by SampleHandlerThread and SampleHandlerThread  is held by the main thread causing a Deadlock. In case of a Deadlock, something is severely wrong in the code which needs to be worked on.One of the ways of finding issues in the code is by inspecting the code. We can inspect the code in android studio by clicking on Analyze -> Inspect Code and this will prompt us to specify the Inspection scope.We can specify the inspection scope to be a file, module or the whole project and perform the inspection. The inspection results will provide a list of various issues such performance issues, threading issues and other severe issues which can be worked on and resolved.

If Bugreports and trace files are not helpful to find the root cause of the ANR then we can use StrictMode to find out what is done wrong on the main thread of the application.

StrictMode is used mainly to detect the network and disk access operations done on the main thread.

StrictMode code example is mentioned below

public class SampleApplication extends Application
{
   @Override
    public void onCreate ()
    {
        super.onCreate();
        .....

        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
             .detectAll()
             .penaltyLog()
            .build());     
     }
}

We can call the StrictMode code in the Application’s onCreate. detectAll() will detect all the possible problems and penaltyLog() will print all the violations done in the logcat.Therefore the logcat has to be monitored for the violations when the application is being used and resolve the violations if they are problematic.

Instead of detectAll() we can specifically use detectDiskReads() and detectDiskWrites()  

to detect disk operations and use detectNetwork() to detect network operations on the Main thread.

Leave A Comment

Your email address will not be published. Required fields are marked *