Understanding Memory Leaks in Android Developments.
Published: 2020-03-09 18:15:25

Imagine living in an apartment with your little waste bin outside, a bit close to the sitting room window. But every time the garbage truck/collectors come around, they skip emptying your waste bin. With each passing day your waste bin gets fuller and more smelly, the pungent smell may discourage you from laying on the sitting room couch. Then one day you decide to do something about your waste, you move your waste bin further to where it could be seen and picked up easily. Finally, your waste gets disposed and you can once again breathe a breath of fresh air.

This is exactly how a memory leak on an android app works. A memory leak occurs due to flimsy mistakes made while coding. And if an app has a memory leakage, the garbage collector can’t clear the heaps. Thereby causing the app to consume more memory which often leads to you receiving an error message stating; “ OutOfMemoryError”.

What is a Memory leak?

A memory leak is a programming error that causes an application to keep a reference to an object that is no longer needed. As a result, the memory allocated for that object cannot be reclaimed, eventually leading to an OutOfMemoryError crash.

One of the core benefits of Java is automated memory management with the help of the built-in Garbage Collector (or GC for short). Now you might want to ask what is a Garbage Collector and its function.

What Is A Garbage Collector?

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/aci6c4uf9uferh7bp1qk.jpeg)

The garbage collector is a program which runs on the Java Virtual Machinewhich gets rid of objects which are not being used by a Java application anymore. It is a form of automatic memory management.

The garbage collector will look for objects which aren’t being used anymore and gets rid of them, freeing up the memory so other new objects can use that piece of memory.

In Java, memory management is taken care of by the garbage collector, but in other languages such as C, one needs to perform memory management on their own using functions such as malloc and free. Memory management is one of those things which are easy to make mistakes, which can lead to what is called memory leaks — places where memory is not reclaimed when they are not in use anymore.

Automatic memory management schemes like garbage collection make it so the programmer does not have to worry so much about memory management issues, so he or she can focus more on developing the applications they need to develop.

###So what are some of the common mistakes that lead to memory leaks?

1. Broadcast Receivers:
Consider this scenario — you need to register a local broadcast receiver in your activity. If you don’t unregister the broadcast receiver, then it still holds a reference to the activity, even if you close the activity.

How to solve this? Always remember to call unregister receiver in onStop() of the activity.

2. Static Activity or View Reference:
You are declaring a TextView as static (for whatever reason). If you reference activity or view directly or indirectly from a static reference, the activity would not be garbage collected after it is destroyed.

How to solve this? Always remember to NEVER use static variables for views or activities or contexts.

NOTE:
Always stop what you started in a program.

How to find a memory leak in our project.

1: Android profiler in android studio
2: LeakyCanary android library.