Android Memory leak

Maqsood Khan
3 min readOct 31, 2019

--

In this article i am going to explain about how memory leak can occur, how many ways it can occur and how to detect and fix these issues.

What is Memory leak?

Memory leak is a scenario that occurs when objects are no longer being used by the application, but the Garbage Collector is unable to remove them from working memory — because they’re still being referenced. As a result, the application consumes more and more resources which eventually leads to a fatal OutOfMemoryError

Many a time we see ANR dialog while using android apps, lags in our apps, we also see OutOfMemoryError in Android Studio while building apps. All these kinds of stuff happen due to memory leaks.
Some objects are not even recognized by garbage collector as garbage. In these situation you can not do anything.

Help garbage collector, help you.

Holding reference of objects that are not required anymore is a bad practice, freeing objects reference after being served is helpful for the garbage collector to kill that object, that eventually helps yourself in memory leaks issues. If you keep objects reference unnecessarily, it only leads to memory leaks.

Memory leaks can happen easily on an android device if not taken care of while building apps, as android devices are provided with very less memory. Memory leaks are the biggest issue for any android app, in spite of being the biggest issue, it is not much difficult to avoid it, if we give importance while building the app. We need to keep few things in mind while building memory leaks free apps.

How many ways memory leak can occur :

  1. Using static Views //private static TextView textView;
  2. Never use context as static //private static Context context;
  3. Avoid using unnecessary singleton classes
  4. Avoid holding context in singleton class
  5. After registering receivers/listeners make sure you should unregister the same after usage
  6. Holding reference of ui specific object in the background
  7. Avoid putting views in collections that do not have clear memory pattern. WeakHashMap store views as values. Since WeakHashMap store views as a hard reference it is better to avoid using it.

These all are the reasons leads to memory leaks and then ANR, lags in app and OutOfMemoryError that ultimately leads to uninstallation of your app by users.

Do not blame garbage collector.

If memory leak happens due to these specific reasons, as the garbage collector is not intended to handle these, these are your own mistakes try not to commit these.

How to detect and Fix

Leak Canary is a library made by Square and it’s a very fast way to detect memory leaks. Leak Canary allows you to detect memory leaks in longer runs because you don’t need to connect your device to the Android Studio and monitor your app for a long period of time. Leak Canary will send you notifications whenever there is a memory leak.

Integration of Leak Canary is really easy. All you need to do is to add the following to your app level build.gradle file:

Add the following code to your Application class:

Detecting Possible Memory Leaks With Infer

Infer is a static analyser tool made by Facebook that helps you find possible null pointer exceptions and resource leaks as well as annotation reachability, missing lock guards, and concurrency race conditions. It’s a good tool to add to your CI to overcome all possible bugs. It’s also open source, so development of the tool is still evolving. You can find more info about Infer in their Quick Start documentation.

Detecting Memory Leaks Using Android Studio’s Monitors

Android Studio provides handy tools for profiling performance of your app, and one of them is the Memory Monitor. We can use the Memory Monitor to detect memory leaks.

Also, Let’s become friends on Linkedin & Github

--

--