Managing Memory Leaks in Android

Posted By : Bipin Tiwari | 22-Apr-2021

What is a Memory leak?
A memory leak is like a virtual oil leak that slowly reduces the available memory that can be used by the system or apps. There may be some codes that unintentionally increase the memory leak and thus making the available memoryless.

To better counter memory leaks we have to understand memory allocation, which is mainly of two types : 

 

1. Stack  : Stack is used basically for Static memory allocation. They are created within a function and are destroyed when the function is complete. These stack objects are temporary and as soon as the function is complete they are released from memory.

 

2. Heap : Heap is used basically for Dynamic memory allocation. They work differently to stacks in the sense that once function is complete, the heap objects are not destroyed and hence will not be reclaimed from memory.

 

Garbage Collector : 

To release objects from Heap memory, Android uses Garbage collector. It simply reclaims the unused objects to free up the memory. GC tracks down each object in the Heap to determine if it's no longer being used or doesn't have any reference and then it releases object from the Heap. 

It is a pretty simple job but the problem arises when we unintentionally keep the reference of an unused object in the heap, and the GC thinks it as an object that is being used and thus doesn't release the object from heap. So we keep storing the reference to unused object and GC can't claim it then eventually the app run out of the memory, which in turn causes apps to freeze or result in an OUTOFMEMORY EXCEPTION which crashes the app.

 

Identifying Memory leaks : 

There are two most used tools to detect memory leaks in Android App.

 

1. Memory Profiler : Android Studio provides a tool called the Memory Profiler for detecting memory leaks. The tool further segments and breaks down memory into portions including Java code, Graphics, Stack, and more to give an overview of what is using up memory as you navigate entirely through the app.

The memory is most useful if you suspect a leak in some specific screen, as it allows you to inspect the allocation and deallocation of objects in heap memory. When using the tool you should use as much as memory is possible like rotating apps or switching between different screens. It allows you to detect which screen is causing the leaks.

You can read more about Memory profiler here on the official site : Memory Profiler 

 

2. Leak Canary : This is a library created by Square, which lets you detect memory leaks. It simply uses an ObjectWatcher class which holds a weak reference to destroyed objects in the heap. In the following five seconds if the reference isn't cleared and GC has already run then the object is considered as retained and thus recorded as a memory leak in Logcat.

You can read more about Leak Canary from the official site : Leak Canary

 

Avoiding Memory Leaks : 
In Android application, most of the time when memory leaks occur is due to the same mistake: Keeping a long-lived reference to a Context.

To avoid context-related memory leaks, follow these practices : 

  1. Don't keep references of Activity of context in static. Otherwise, your references might still be there even if you need it.
  2. Avoid keeping references of ImageViews. Recycle / Clear the bitmap inside when you destroy the view.
  3. Use context application instead of a context activity. If you want to have a reference of the context, we recommend using context.getApplicationContext().

Always remember : A garbage collector is not insurance against memory leaks.

For any project discussion, connect with us.

 

About Author

Author Image
Bipin Tiwari

Bipin is a highly skilled Android developer with extensive professional experience in creating innovative and efficient mobile applications. He possesses proficiency in programming languages such as Java and Kotlin, along with a deep understanding of Android SDK, design patterns, and best coding practices. Bipin is committed to delivering high-quality code and ensuring exceptional user experiences. With excellent problem-solving skills and the ability to work collaboratively in a team environment, he has made valuable contributions to both client and internal projects, including the development of the Blackbook Travels App and Corniz App.

Request for Proposal

Name is required

Comment is required

Sending message..