How Does OutOfMemory Error Happen and How to Solve it in Android?

In continuation with the last post on understanding the OutOfMemory Error, we now examine the primary causes of the OOM Error in Android Studio. OutOfMemoryError, sometimes known as OOM, is an error faced by every Android developer. In the near future, you will face an OOM in your Android application if you haven’t already. In Android, the OutOfMemoryError is caused by memory leaks. To eliminate the OutOfMemoryError from your Android application, you must first reduce memory leaks.

What Could Be the Cause of an OutOfMemoryError?

Android OutOfMemoryError can arise for a variety of reasons. The following are some of the most common causes of OutOfMemoryErrors caused by Memory Leaks:

  • The inner class that isn’t static
  • Use of getContext() and getApplicationContext() incorrectly()
  • Application of a static view, context, or activity

Let’s go through each of them One by One:

1. Inner class that isn’t static

Make any nested classes in your application static because static classes do not need an implicit reference to the outer class. If the inner class is made non-static, the outer class will remain active until the application is active. This can result in an OutOfMemoryError if your class requires excessive memory. Therefore, it is preferred that the inner class be static. In Java, the inner class must be made static manually, whereas in Kotlin, the inner class is static by default. You need not bother about static inner classes in Kotlin.

Also Read: 10 Tips to Fix iPhone Stuck in Headphone Mode Like a Pro!

2. Use of getContext() and getApplicationContext() incorrectly()

The majority of the third-party libraries utilised by our application are Singletons. If you must provide context to a third-party library outside the scope of the current activity, use getApplicationContext() instead of getContext (). Similar to the Example below, initialise is a static library method that uses the context

However, some libraries do not employ the aforementioned notation. So, in that case, it will use the context of the current activity, and the current activity’s reference will be preserved as long as the application is alive, which may cause an OutOfMemoryError (as the initialise function is static).

It is therefore recommended to use getApplicationContext() directly in your code as opposed to relying on third-party libraries. These are various approaches for removing the OutOfMemoryError error from our application. Writing code that does not result in an OutOfMemoryError is preferable.

Nonetheless, if your project is vast and you’re having difficulties locating the class that generates OutOfMemoryError, you can use Android Studio’s memory profiler to identify the offending classes.

Also Read: How to change HEIC to JPG in iphone?

3. Application of a static view, context, or activity

OutOfMemoryError will occur if any static views, contexts, or activities are utilised (if your activities are dealing with lots of space). This is due to the fact that the programme will retain the view, context, or activity until the application is still active, and the memory consumed by these will not be released by the Garbage Collector.


Leave a Comment