1 of 11

Memory Leaks

Mayokun Yusuf. June 05, 2025

Memory Leaks in Android: Detection and Prevention

2 of 11

Introduction

What are Memory Leaks?

  • In Android, memory leaks usually refer to reachable objects that are no longer useful. For example, if your app maintains a reference to an Android activity that has been destroyed, it's considered a leak, because the activity can't be displayed again, but GC cannot reclaim it because it is still owned by your app.
  • Why are they Critical in Android?
  • Android devices have limited memory resources compared to desktop computers.
  • Unreclaimed memory leads to performance degradation, crashes (Out Of Memory errors), poor battery performance and a poor user experience.

3 of 11

Understanding Memory Leaks

A recent study found memory leaks in 39% of the

top 100 Android apps on Google Play.

​

Common Causes:

​

Static fields – 28%

Unclosed resources – 24%

Inner classes – 18%

Other causes – 30%

​

​

For example, Pokemon Go suffered from memory leaks, leading to a 12% increase in crash rates and 23% more users reporting performance issues.

4 of 11

Garbage Collection (GC)

GC is the automatic process used by Android to reclaim memory that is no longer in use, so that your app doesn’t run out of memory and crash.

What GC Does

  • Tracks objects in memory.
  • Identifies unused objects that can no longer be accessed by the app.
  • Frees up memory by removing those unused objects.

​

How GC Works in Android (Dalvik & ART)

Android apps run on the ART (Android Runtime) or older Dalvik VM, both of which use automatic garbage collection. Here's how it generally works

  • Allocation: Every object you create (e.g., new User()) is stored in the heap memory.
  • Reachability Analysis: The GC periodically scans the heap to find objects that are no longer referenced.
  • Deallocation: These unreachable objects are cleared to free up space

5 of 11

Causes of Memory Leaks

1. Static references to Context (Activity/Fragment)

​

​

Why is this a memory leak?

  • Static Reference Keeps Object Alive
  • Lifecycle Mismatch

2. Handlers and Runnables: Delayed messages or runnables may keep the Activity in memory even after it’s destroyed.

​

6 of 11

Causes of Memory Leaks

3. Views not released (e.g., Bitmaps): ImageViews holding large bitmaps can leak if not explicitly released.

​

​

​

4. Singletons holding Context Avoid using Activity context in singletons. Use ApplicationContext instead.

​

​

​

​

​

5. Leaking Fragments / Activities FragmentManager leaks due to retained fragments or mismanagement.

​

​

​

7 of 11

Causes of Memory Leaks

6. WebView leaks Improper WebView lifecycle management can cause memory leaks.

​

​

7. Listeners and Observers Forgetting to unregister listeners, broadcast receivers, or LiveData observers

​

​

8. Inner classes / anonymous classes. Non-static inner classes hold implicit references to outer classes.

8 of 11

Preventing Memory Leaks

  • Use ApplicationContext where possible (e.g. in singletons).
  • Avoid non-static inner classes.
  • Use Lifecycle-aware components like ViewModel, LiveData.
  • Clean up in onDestroy.

​

  • Don’t store Context or Views in long-lived objects.
  • Use WeakReference when holding optional references.
  • Unregister BroadcastReceivers and listeners

​

​

  • Release Bitmap resources

9 of 11

Detecting Memory Leaks

​

  • Monitor logcat for OutOfMemoryError or ANRs.
  • Use heap dumps and analyze object references.
  • LeakCanary: Automatically detects and reports leaks.
  • Android Profiler: Visualize memory usage, capture heap dumps.

10 of 11

Detecting Memory Leaks

  • LeakCanary

Add the dependency to your build.gradle (app-level):

​

Build and run the app

LeakCanary auto-hooks into the Application class.

When an Activity/Fragment is destroyed but not GC’d, it logs and shows a leak notification

View Leak Report

11 of 11

Detecting Memory Leaks

  • Android Profiler

Open Android Studio.

Run your app on an emulator or physical device.

Click View > Tool Windows > Profiler.

​

​

THANK YOU.