The Memory Leak Prevention skill teaches your AI coding agent the patterns that prevent Activity, Fragment, and View leaks across both View-based and Jetpack Compose UI systems. Memory leaks accumulate across screen navigations, cause GC pressure and jank, and eventually produce OutOfMemoryError crashes on mid-range devices.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/AmolPardeshi99/android-performance-skills/llms.txt
Use this file to discover all available pages before exploring further.
What is a memory leak on Android?
A memory leak is an object that is no longer needed by the app but cannot be freed by the garbage collector because something else still holds a strong reference to it. ART’s tracing GC walks every reachable reference starting from GC roots — static fields, thread stacks, JNI globals. Anything reachable from a root survives collection. Leaks happen when a GC root (a singleton, a static field, a running thread) keeps a reference chain alive to an object — usually an Activity, Fragment, or View — that should have been collected.Why leaks matter on Android
- The heap is constrained per process; a leaked Activity carries its entire View tree (often MBs).
- Leaks accumulate across screen navigations → GC runs more frequently → jank.
- Enough leaks →
OutOfMemoryErrorcrashes or ANRs from GC pauses. - Leaked processes consume device RAM, causing LMK (Low Memory Killer) to terminate other apps.
Topics in this section
GC Model
How ART’s tracing garbage collector works, what GC roots are, and why a single leaked Activity can retain 5–50 MB of heap.
View System Leaks
Fragment ViewBinding lifecycle, onDestroyView nulling, and avoiding View tree retention on the back stack.
Context & Static Refs
applicationContext vs Activity context, singletons holding UI references, and static field leak patterns.
Listeners & Observers
Symmetric registration and unregistration for listeners, BroadcastReceivers, LiveData, and Flow collection.
Resources & Cleanup
Closing Cursors, InputStreams, MediaPlayer, Bitmaps, and SQLite resources at the right lifecycle stage.
Compose Leaks
remember scoping, LocalContext misuse, and composable lambdas captured inside ViewModels.
Compose Side Effects
DisposableEffect, LaunchedEffect, rememberCoroutineScope, and RememberObserver cleanup patterns.
LeakCanary
Setting up LeakCanary, reading leak traces, and integrating detection into your CI pipeline.
Profiling Tools
Android Studio Memory Profiler, heapprofd, Perfetto heap graphs, and ApplicationExitInfo OOM detection.