How to Detect and Debug Memory Leaks in Android Without LeakCanary
Short answer
How to find and fix a memory leak in Android using only Android Studio: reproduce the leak, open the Memory Profiler, force garbage collection, capture a heap dump, follow the reference chain from the GC root to the retained object, fix it, and repeat the same steps to verify the fix.

Memory leaks are among the most common performance issues in Android applications. They can lead to increased memory consumption, unnecessary object retention, sluggish UI performance, and eventually OutOfMemoryError.
Most Android developers are familiar with LeakCanary, which automatically detects and reports memory leaks. But what if LeakCanary isn't available in your project?
Can you still identify a memory leak, find the object responsible for it, and understand why it is happening?
Absolutely.
Android Studio provides a Memory Profiler and Heap Dump analysis tools that allow you to investigate memory leaks manually.
Let's understand the complete debugging process.
1. First, Understand What a Memory Leak Is
A memory leak occurs when an object that is no longer needed remains reachable through another object.
The Garbage Collector (GC) removes objects that are no longer reachable from GC Roots.
However, if an object is still reachable, the GC cannot collect it, even if the application no longer needs it.
Consider this example:
object ProductManager {
var activity: Activity? = null
}
class ProductActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
ProductManager.activity = this
}
}Here, ProductManager is a singleton. It holds a reference to ProductActivity.
Now imagine the user navigates away from ProductActivity, and Android calls its onDestroy() method.
The Activity is destroyed from the lifecycle perspective, but ProductManager still holds its reference.
Therefore, the Activity remains reachable and cannot be garbage collected.
The reference chain looks like this:
GC Root
|
v
Static Singleton
|
v
ProductManager
|
v
ProductActivityThe important thing to understand is that calling onDestroy() does not automatically make an Activity eligible for garbage collection.
Step 1: Reproduce the Suspected Memory Leak
Before opening Android Studio Profiler, reproduce the issue.
For example, suppose you suspect that ProductActivity is leaking.
Follow these steps:
Launch the application.
Open
ProductActivity.Navigate back and close the Activity.
Repeat this process 5–10 times.
Why repeat the process?
If an Activity is properly released, its destroyed instances should eventually become eligible for garbage collection.
But if each navigation leaves another Activity instance retained in memory, you may have a leak.
Repeatedly reproducing the same scenario makes the problem easier to observe.
Step 2: Open Android Studio Memory Profiler
Android Studio provides a Memory Profiler that helps you monitor application memory consumption.
Open:
Android Studio → View → Tool Windows → Profiler
Select your running application and open the Memory Profiler.
You can observe different memory-related metrics, including:
Java/Kotlin heap memory
Native memory
Graphics memory
Stack memory
Total memory consumption
For investigating Activity leaks, Java/Kotlin heap analysis is particularly useful.
Look for patterns where memory consumption keeps increasing after repeatedly opening and closing the same screen.
However, increasing memory usage alone does not prove a memory leak.
You need to inspect which objects are being retained.
Step 3: Force Garbage Collection
In the Memory Profiler, use the Force GC / Initiate Garbage Collection action.
Why do we need this?
The Garbage Collector removes objects that are no longer reachable from GC Roots.
By triggering GC before taking a Heap Dump, you can reduce the number of objects that are simply waiting for garbage collection.
Remember:
Calling System.gc() or manually triggering GC does not guarantee immediate collection of every eligible object.
The GC decides when and how collection happens.
Step 4: Capture a Heap Dump
This is one of the most important steps in memory leak debugging.
A Heap Dump is a snapshot of objects currently present in the application's heap, along with their references.
In Android Studio:
Open Memory Profiler.
Trigger Garbage Collection.
Click Dump Java Heap.
Wait for Android Studio to analyze the heap.
Search for your suspected Activity class, such as
ProductActivity.
You can inspect how many instances of that Activity currently exist.
For example:
ProductActivity
Instances: 5If you repeatedly opened and closed the Activity five times, but all five instances are still retained after GC, that is a strong indication that something is holding references to them.
But don't jump to conclusions.
Some objects may legitimately remain alive. You need to inspect their reference chains.
Step 5: Inspect the Reference Chain
This is where you identify the actual cause of the leak.
Select the suspected Activity instance in the Heap Dump and inspect its references.
Look for the Path to GC Roots or equivalent reference-chain information in the heap analysis view.
For example:
GC Root
|
v
Static Singleton
|
v
ProductManager
|
v
ProductActivityThis tells us:
The GC Root keeps the singleton reachable.
The singleton references
ProductManager.ProductManagerholds a reference toProductActivity.Therefore, the Activity is still reachable.
Now we have identified the problem.
The issue is not necessarily inside ProductActivity itself. The actual cause is the long-lived ProductManager retaining its reference.
This is an important distinction in memory leak debugging:
The object being leaked and the object causing the leak are not always the same.
Step 6: Fix the Memory Leak
In our example, the problem is that a singleton holds an Activity reference.
One possible fix is to clear the reference when the Activity is destroyed:
override fun onDestroy() {
ProductManager.activity = null
super.onDestroy()
}However, a better approach is to avoid storing Activity references in long-lived singleton objects in the first place.
If an application needs a Context for an application-level operation, use applicationContext where appropriate.
The correct solution depends on the ownership and lifecycle of the object.
Other common fixes include:
Unregister listeners when they are no longer needed.
Remove callbacks from Handlers.
Cancel unnecessary long-running coroutines.
Clear Fragment ViewBinding references in
onDestroyView().Avoid retaining Views or Activities in static fields.
Step 7: Verify the Fix
Never assume that fixing the suspected reference has resolved the leak.
Repeat the same debugging process:
Reproduce the same navigation scenario.
Trigger Garbage Collection.
Capture another Heap Dump.
Search for
ProductActivity.Inspect whether destroyed instances are still retained.
If the old Activity instances are no longer retained after GC, the fix is working as expected.
Common Android Memory Leak Scenarios
Here are some common situations worth investigating during debugging.
Scenario | Possible cause |
|---|---|
Activity leak | Singleton holding Activity reference |
Fragment leak | ViewBinding not cleared |
Handler leak | Delayed Runnable retaining Activity |
Listener leak | Listener not unregistered |
Coroutine leak | Long-running coroutine retaining references |
View leak | View retained by a long-lived object |
For example, in a Fragment, forgetting to clear ViewBinding can retain the Fragment's View hierarchy longer than necessary.
private var _binding: FragmentHomeBinding? = null
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}Important Interview Questions
Q1. If you force GC, will all non-reachable objects be removed?
Objects that are no longer reachable become eligible for garbage collection. However, collection is not guaranteed to happen immediately.
Q2. How do you identify which object is causing the leak?
Inspect the reference chain from the GC Root to the retained object. Find the long-lived object that is unnecessarily holding the reference.
Q3. What if multiple Activity instances appear in a Heap Dump?
Check whether those instances are expected to be alive. If destroyed instances are still retained after GC, investigate their reference chains.
Q4. Does high memory consumption always indicate a memory leak?
No. High memory usage can also result from caching, large images, temporary allocations, or legitimate object retention.
Q5. Is LeakCanary the only way to detect memory leaks?
No. Android Studio Memory Profiler, Heap Dumps, and reference-chain analysis can be used to investigate memory leaks manually.
Understanding memory leaks is not just about knowing how to use LeakCanary.
As an Android developer, you should understand:
How Garbage Collection works.
What GC Roots are.
Why reachable objects cannot be collected.
How to capture and analyze Heap Dumps.
How to trace references back to their root.
How to identify and fix unnecessary object retention.
The most important debugging skill is not finding an object that remains in memory. It is understanding why that object is still reachable.
Once you understand reference chains and object ownership, debugging memory leaks becomes much more systematic.
Frequently asked questions
What is a memory leak in Android?
A memory leak occurs when an object that is no longer needed remains reachable through another object. The garbage collector only removes objects that are no longer reachable from GC roots, so a still-reachable object cannot be collected even if the app no longer needs it.
Why force garbage collection before capturing a heap dump?
It reduces the number of objects that are simply waiting to be collected, so the heap dump shows fewer false suspects. Forcing GC does not guarantee that every eligible object is collected immediately, because the garbage collector decides when and how collection happens.
How do you find which object is causing a memory leak?
Inspect the reference chain from the GC root to the retained object, and find the long-lived object that is unnecessarily holding the reference. In the post's example, a singleton holds a reference to an Activity.
Comments (0)
No comments yet. Be the first to share your thoughts!
Leave a Comment
Get the latest updates
Join our newsletter and never miss an article.