Home > database >  Is there a way to ahead detect ANR programmatically in production?
Is there a way to ahead detect ANR programmatically in production?

Time:02-20

StrictMode is designed to be used in development and is disabled by default without developer mode, so I'm wondering if there is a way to detect ANR condition ahead, in order to intervene properly before OS ends in ANR timeout that under a specific situation is long and certainly will conclude with an annoying and generic message "App doesn't respond" after a waste of time for nothing.

CodePudding user response:

No. In fact its mathematically provable you can't. What you're asking is the equivalent of the Halting Problem- a mathematical question of whether or not its possible to write a program that can detect if another program will ever end or not. Alan Turing proved that it's impossible for the general case back in the 30s.

Strict mode does something much smaller than that. It defines conditions in order to do certain things (like you can't be on the main thread to make a socket call) and checks those conditions before it does those things. You could definitely increase the amount of things strict mode can catch, but you can never make it catch all ANRs.

CodePudding user response:

LeakCanary is a memory leak detection library for Android.

LeakCanary’s knowledge of the internals of the Android Framework gives it a unique ability to narrow down the cause of each leak, helping developers dramatically reduce OutOfMemoryError crashes

Use this link: https://square.github.io/leakcanary/

  • Related