Android & Jetpack Compose (50 Qs)•Part 3: ART Internals, Memory Leak Profiling & Production Hardening (25 - 35)
Memory Leak Profiling: LeakCanary architecture and common Activity retain leaks.
senior LevelPerformanceMemory ManagementSystem Design
In Simple Words: A memory leak occurs when an object is no longer needed (e.g. an Activity destroyed after back press) but remains referenced by a GC Root, preventing garbage collection:
- Common Causes: Static variables holding Context references, non-static inner classes, un-cancelled RxJava/Coroutine subscriptions, or singleton listeners.
- LeakCanary: Automatically watches destroyed Activities and Fragments using
WeakReference. If the object is not collected after 5 seconds, it forces a garbage collection and dumps the HPROF heap file, analyzing and displaying the exact reference chain causing the leak in a notification!