ANR & Memory Management Interview Questions

PERFORMANCE › Runtime

What is an ANR, and how do you prevent one?

What a strong answer covers: An Application Not Responding error happens when the main thread is blocked: input not handled for 5 seconds, a broadcast receiver or service taking too long. Prevent by keeping disk, network, database and heavy computation off the main thread, avoiding locks the main thread waits on, doing less in Application.onCreate and lifecycle callbacks, and using StrictMode in debug to catch disk and network access on Main.

What causes memory leaks on Android, and how do you find and fix them?

What a strong answer covers: A leak is an object kept reachable after it's no longer needed, typically an Activity or Fragment view held by a static field, a singleton, an unregistered listener, a long-running coroutine or thread, or an inner class callback. Detect with LeakCanary and Memory Profiler heap dumps; fix by unregistering in the matching lifecycle callback, using lifecycle-aware components, application context in singletons, and scoped coroutines.

How would you investigate an ANR reported in Play Console that you can't reproduce?

What a strong answer covers: Read the ANR traces in Play Console or Crashlytics: the main thread's stack shows what it was blocked on, such as a lock, I/O, Binder call or SharedPreferences. Look at other threads to see who holds the lock. Group by device and OS version, check ApplicationExitInfo for the reason (API 30+), add trace sections around suspects, and use StrictMode and a slow-device emulator to reproduce.

What is the difference between a leak and high memory usage, and how do you reduce memory use?

What a strong answer covers: A leak is memory that grows because unused objects stay reachable; high usage can be legitimate but too large, like full-size bitmaps or large caches. Reduce usage by sizing images correctly, bounding caches (LruCache based on memory class), avoiding duplicate copies of data, releasing resources in onTrimMemory, and paging large lists. Check the Java heap, native heap (bitmaps) and graphics memory in the profiler.

Your app's ANR rate is above Play's bad behaviour threshold. How would you plan bringing it down?

What a strong answer covers: Get data first: group ANRs by stack signature and rank by user impact. Typical big wins: moving synchronous disk access (SharedPreferences commits, database, file reads) and Binder calls off Main, fixing lock contention, slimming Application.onCreate and content provider initialisation, and fixing broadcast receivers doing work in onReceive. Add StrictMode and tracing, monitor ANR rate per release, and gate releases on it. The threshold is 0.47 percent user-perceived ANR rate.

Back to ANR & Memory Management