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

19 Jun, 2021
Continuing our journey from understanding the OutOfMemory Error from the previous article we now check on the main reasons which cause the OOM Error in Android Studio. Every Android developer must have encountered the OutOfMemoryError, sometimes known as OOM. If you haven’t yet encountered an OOM in your Android application, you will in the near future. Memory leaks cause the OutOfMemoryError in Android. To eliminate the OutOfMemoryError, you must first eliminate memory leaks from your Android application.
The Error!
When you run an application on an Android device, the Android system allocates memory to it in order for it to function. All variables, functions, and activity creation, for example, takes happen alone in that memory.
Example: If the Android System assigns 200MB to your program, for example, your application can only use 200MB at a time. If the amount of space allotted to the program is reduced over time, or if very little space is available, the Garbage Collector (GC) will release the memory held by variables and activities that are no longer in use. As a result, the application will get some space once more.
However, if you build your code in such a way that it contains references to objects that are no longer necessary, the Garbage Collector will be unable to release the unused space, and the application will run out of space. This is referred to as a Memory Leak.
We encounter the OutOfMemoryError in Android due to the phenomenon of Memory Leak in Android because your code is holding references to objects that are no longer required and the Garbage Collector is unable to perform its job, and your application consumes all of the space allocated to it by the Android System and demands more.
OutOfMemoryError in Android can occur for a variety of reasons. The following are some of the most typical causes of Memory Leaks that result in an OutOfMemoryError:
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
If you have a nested class in your application, make it a static class because static classes don’t require an implicit reference to the outer class. If you make the inner class non-static, the outer class will be active till the application is active. If your class uses a lot of memory, this can result in an OutOfMemoryError. As a result, it’s preferable to make the inner class static. In Java, you must manually make the inner class static, whereas, in Kotlin, the inner class is static by default. In Kotlin, you don’t have to worry about static inner classes.
2. Use of getContext() and getApplicationContext() incorrectly()
In our application, we use a lot of third-party libraries, and the majority of them employ the Singleton class. If you need to give some context to a third-party library that is beyond the scope of the current activity, use getApplicationContext() rather than getContext(). Like in the Example below initialize is a static function in the library that uses the context
Java
SomeLibrary{    object companion    {        initializeGeeksforGeeksLib(Content context)        {            this.context = context.getApplicationContext()        }    }}
Some libraries, however, do not utilize the aforementioned notation. So, in that scenario, it will use the current activity’s context, and the current activity’s reference will be retained till the application is alive, which may result in an OutOfMemoryError (as the initialize function is static). As a result, it is preferable to utilize getApplicationContext() directly in your code rather than relying on third-party libraries. These are some of the methods for eliminating OutOfMemoryError from our application. It is preferable to write code that does not result in an OutOfMemoryError. Still, if your project is large and you’re having trouble finding the class that causes OutOfMemoryError, you can utilize Android Studio’s memory profiler to locate the classes that cause OutOfMemoryError.
3. Application of a static view, context, or activity
OutOfMemoryError will occur if you use any static views, contexts, or activities (if your activities are dealing with lots of space). This is because the program will hold the view, context, or activity until the application is alive, and the memory used by these will not be released by the Garbage Collector.
Example: If you’re going to use Bitmap in some project. That project will meet the OutOfMemoryError on your mobile device if you use more than 10 photos with a size of 500KB each. The number of images that can cause an OutOfMemoryError in other situations can be greater than or fewer than 10. You may ascertain the limit by adding photos to the BitmapArray one by one until you get an OOM, which indicates the device’s limit.
Picked
Android
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.