Codementor Events

Memory Leaks in Android: Find, Fix, and Avoid

Published Aug 04, 2021
Memory Leaks in Android: Find, Fix, and Avoid

Nobody likes it when apps glitch and freeze. Especially when it happens at the most inconvenient moment. When we book a taxi, bet on a game at the last second, or turn into the wrong lane while using maps navigation on our phones. All this makes people frustrated and creates a bad experience with the product, brand, apps, and as a result, the download statistics go down.

Every developer knows, “When download statistics go down, it’s time to analyze why crash and ANR statistics are up”.
One of the reasons for glitches, crashes and freezes can be memory leaks. Now we will talk about how to identify them in Android applications and how to deal with them.

What are memory leaks?

Most Android native apps are written on Java or Kotlin. That means that we have a Virtual Machine with a Garbage Collector (GC).

Garbage Collection deals with finding and deleting the garbage from memory. However, in reality, Garbage Collection tracks each and every object available in the JVM heap space and removes unused ones.>

A memory leak happens when memory is allocated but never freed. This means the GC is not able to take out the trash once we are done with the takeout.
Android has a 16ms drawing window, and the GC normally takes less time to deal with memory. When the heap of allocated memory is increasing in unevenly large portions and memory is not being deallocated, the system will force a larger GC to kick off, which pauses the entire application’s main thread for around 50ms to 100ms.

Does my application have memory leaks?

First of all, we need to find and detect memory leaks in the application. Let’s look over some of the ways and tools you can use to do that.

Android Studio Memory Monitor

Android Studio provides handy tools for profiling the performance of your app. One such tool is the Memory Monitor. Open the bottom tab in Android Studio while the app is running on a device or emulator. You can see how much memory your app has allocated at the moment. Memory heap will be increasing and decreasing depending on the actions that the application does at the moment. If you notice that memory is allocated but doesn’t get deallocated in a short period of time, this is probably what we are looking for.

Infer

Infer is a static analyzer tool made by Facebook. This CI tool helps you find possible null pointer exceptions and resource leaks, as well as annotation reachability, missing lock guards, and concurrency race conditions. You can read more on the getting started page.

ANR statistics on Google Play Console

Some of ANR (“Application Not Responding”) may be caused by big heap allocating and UII freezes for more than 50 – 100ms. You can check them on Google Play Consoles or read more here.

Crashes with OutOfMemmoryException

This is a 100% proven indicator that you have a memory leak. You should check all the resources and Context-related variables used in classes that you see on the crash stack.

LeakCanary

With its vast knowledge of the Android Framework internals, LeakCanary has a unique ability to narrow down the cause of each leak. This helps developers dramatically reduce OutOfMemoryError crashes. You can read more on the getting started page.

Avoid Memory Leaks in Android

If you want to avoid memory leak scenarios, you should incorporate in your work these best practices:

  • Take care of context.
  • Use common architectures for Android projects, such as Clean, ViewModel, or UseCase Architectures. A clean and structured project is the best way for avoiding most issues. Here is the official architecture guide from Google.
  • Delegate referencing to DI and inject context-related references.

Read how to fix the memory leaks issue if you found them alread in the original post about solving memory leaks in Android.

Discover and read more posts from Tetiana Mykhailyk
get started
post commentsBe the first to share your opinion
Show more replies