Android’s ART runtime uses a tracing garbage collector that walks every reachable reference starting from a fixed set of GC roots. Any object reachable from a root survives collection — a leak is simply an object that remains reachable even though the app no longer needs it. Understanding this model is the foundation for diagnosing every type of memory leak.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.
“Reachable” and “no longer needed by the app” are two different things. The GC only knows about reachability — it cannot know whether your app still intends to use an object. An Activity that has been destroyed is no longer needed, but if a static singleton holds a reference to it, the GC still considers it reachable and will not collect it. This is the essence of every Android memory leak.
0.1 How ART garbage collection works
ART uses a tracing GC: starting from a fixed set of GC roots, it walks every reachable reference chain. Any object reachable from a root survives; anything not reachable is collected. A leak is simply an object that is still reachable from a root even though the app no longer needs it. GC roots on Android:| Root type | Examples |
|---|---|
| Static fields | companion object fields, object singletons, Java static variables |
| Thread stacks | Local variables and parameters on any running thread’s call stack |
| JNI globals | References held by native code via NewGlobalRef |
| Running threads | The Thread object itself, plus everything it references |
| System class loader | Class objects and their static fields |
0.2 Java/Kotlin reference strength
Not all references are equal. Java and Kotlin provide four reference strengths that control when the GC is allowed to collect the referenced object.| Reference type | GC behaviour | Android use case |
|---|---|---|
| Strong (default) | Object survives as long as the reference exists | Everything unless you explicitly weaken it |
| SoftReference | Collected only when JVM is low on memory | Image caches — JVM decides when to evict |
| WeakReference | Collected at next GC cycle when no strong refs remain | Safe back-reference from long-lived object to Activity |
| PhantomReference | Collected after finalization; never returns object | Low-level cleanup hooks; rarely used in app code |
WeakReference when a long-lived object needs to call back into a shorter-lived one (such as an Activity). If the Activity has been destroyed and collected, get() returns null and the call is safely skipped:
0.3 Why Activity/Fragment leaks are especially costly
A single leakedActivity retains:
- Its entire View tree (every
TextView,RecyclerView,ImageView, etc.) - Every Bitmap loaded into those views
- The Context — which itself references the
WindowManager,LayoutInflater, and theme resources - Any ViewBinding object referencing all of the above