Skip to main content
ANR and how should we resolve that?

ANR ,do you know how to fix it?

Where To Look For ANR?

  • The app is doing slow operations on main thread. (Including I/O)
  • App is doing a long calculation on main thread.
  • The main thread is doing a synchronous binder call to another process and that process is doing a heavy job.
  • The main thread is blocked waiting for another process which is doing a long job.
  • The main thread is in a deadlock with another thread.

Fixes

  • StrictMode is a developer tool which detects things you might be doing by accident and brings them to your attention so you can fix them. StrictMode is most commonly used to catch accidental disk or network access on the application's main thread, where UI operations are received and animations take place. Keeping disk and network operations off the main thread makes for much smoother, more responsive applications. By keeping your application's main thread responsive, you also prevent ANR dialogs from being shown to users.
  • Developer Options Actually not all ANR’s are shown to user. But at Developer Options of Settings, there is an option “Show All ANRs”. If this option is selected, Android OS will show you internal ANRs also.
  • TraceView Android Studio has an inbuilt tool called TraceView. You can see memory and CPU usage of your app. For how to user TracevView, please refer to here.
  • Inspect Traces File When ANR happens, Android logs some info related to the case in a txt file on device itself. You can use ADB to gather logs and have a look. Just use theese commands.

Comments

Popular posts from this blog

Looper What is looper? Looper is a class which is used to execute the Messages(Runnables) in a queue. Normal threads have no such queue, e.g. simple thread does not have any queue. It executes once and after method execution finishes, the thread will not run another Message(Runnable). Where we can use Looper class? If someone wants to execute multiple messages(Runnables) then he should use the Looper class which is responsible for creating a queue in the thread. For example, while writing an application that downloads files from the internet, we can use Looper class to put files to be downloaded in the queue. How it works? There is prepare() method to prepare the Looper. Then you can use loop() method to create a message loop in the current thread and now your Looper is ready to execute the requests in the queue until you quit the loop.