AndroIdiots Podcast 8: Memory Leaks

Tushar Gupta
Sep 24, 2018 · 4 min read
A small leak can sink a great ship. — Benjamin Franklin

Often early in our careers, We are so focused on developing new Apps with new features that we never actually care about the performance of our apps. This leads to technical debt and often degrades the quality. One of the contributing factor to poor performance is Memory leak. In the 8th episode of AndroIdiots Podcast we discuss Memory leaks in Android and how to avoid them with Jupneet .

Show Notes:

Shouldn't Java solve this for us?

Every object has got its own lifetime, after which its memory is deallocated. If some other object is holding onto this object, then GC will not be able to collect it and clear the memory. So when an Object's lifetime gets extended beyond its purpose, its then we get a memory leak.

What happens when memory leaks?

What causes Memory Leak?

  1. Static Context or Static Views:
    Static members are referenced by their classes and are not cleared by GC. A static view (a view in turn holds on to an Activity/Fragment) or a static context, which are quite heavy objects, will remiain in memory until the User proactively kills the application hampering the App Experience.
    Static objects are cleared off sometimes but its not certain. Read this for further explanation.
  2. Inner Classes or Anonymous Classes:
    A very useful feature of Java when misused can cause potential memory leak. We have mentioned both in a single point because Anonymous classes are same as Inner Classes. They both implicitly hold on to the outer class reference. If an Inner/Anonymous class outlives the outer class, the latter will not be cleared.
  3. Unregistered Listeners:
    A lot of times, we register for listeners but forget to unregister it. A very common example is registering for Location Updates. All we need to do is to get the LocationManager system service and register a listener for location updates. (say in an Activity). Now, LocationManager keeps a strong reference of the Activity. If we do not unregister, GC will not be able to remove Activity instance from memory because LocationManager is still holding on to it.
  4. Bitmaps:
    Bitmap is the in-memory representation of pixel data of an image. This can be huge at times. We have so many open source libraries like Glide, Fresco, Picasso that we usually don't have to worry about Bitmaps. But if we do, we need to be cautious because these are heavy objects. Check the following links for a better understanding of Bitmaps:
    a) Managing Bitmap Memory
    b) How Image Loading Libraries work
    c) Reusing Bitmap

How to detect Memory Leaks?

  1. Android Studio Memory Profiler can help you identify usage peaks.
  2. Understanding how references work in Java, can help avoid Memory Leaks. For ex: When passing Context or View to an AsyncTask always use weak reference.

A bonus read: How Garbage Collection really works.

That's it folks. In this episode, we tried to understand Memory leaks and how to avoid them. Hope you enjoyed it.

You may follow our Subject Matter Experts on these platforms:

Follow us on twitter for regular udpates and feel free to DM the hosts (Anupam & Tushar) for any queries.


An android developer publication to stay updated with whats new in android, best practices and how to become a better android developer

Tushar Gupta

Written by

Self-Taught iOS & Android Dev | co-creator AndroIDIOTS | Functional Programming enthusiast | Enjoy diving deep into Architecture & Programming Languages.


An android developer publication to stay updated with whats new in android, best practices and how to become a better android developer

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade