Android Memory Optimization: Best Practices for Better Memory Management

android memory optimization

Random-access memory (RAM) is a vital resource in every software development system, but it is even more important on a mobile operating system where physical memory is usually occupied.

There are broadly two types of memories in Android.


Clean RAM

Android prefers paging and memory-mapping to make memory available to the system when required. All files and resources present on the disk like code is kept in mapped pages as these pages can be easily recovered from disk hence these can be paged out and memory can be released for the system.


Dirty RAM

Dirty RAM is the memory that cannot be paged out as well proves to be expensive if running in the background. Maximum memory inside a running application is dirty memory and this is the one that can be used for memory optimization.


Android Memory Overview

All applications are recognized by android as running process or cached process. Android kills one or multiple cached processes when there is requirement of memory for running process. The process killed under cached process are in LRU (least recently used) order.



Memory Optimization: Best practices for enhanced memory usage

Keeping your system from running out of memory helps in boosting the overall memory usage and performance of Android thereby ensuring delightful user experience.


1. Avoid Memory Leaks at all cost

Prominent reason for memory leak is not freeing up the allocated memory before all related references go out of scope. When references to those objects which are no longer required in the app are not released then it leads to logical memory leaks.


In case a very strong Logical memory leaks, on the other hand, are the result of forgetting to release references to objects that are no longer needed in your app. If a strong reference to an object still exists, the garbage collector cannot remove the object from memory.

memory Leak

Image credit:

It can be avoided by following ways:

a- Cursor should be closed after the database query is finished.

b- It is recommended to call unregisterReceiver() after calling registerReceiver().

c- An Activity instance  leakage can result of significant memory loss thereby causing increased OOM(out of memory). This can be avoided as following:

– Never keep long-lived references to a context-activity.

– Always prefer to replace the context-activity with context-application.

d- AsyncTask should be used for short-lived operations.


2- Profile guided Dex file layout

dex file

image credit : Android Developers

Application is inside an APK file where multiple dex files (contains instruction for application execution) are present and upon launching an application the dex file is read by device into memory with several hundred pages being loaded into the memory. Generally, developers are not aware about important use cases of a specific user so there are all a lot of unimportant things present along with important things.

In order to improve Dex file layout use the JIT profile information to move important things close to each other as shown below:

 dex file

image credit : Android Developers


In above image it is evident that a lot less pages are accessed now after using the profile information.

Below snapshot showcase a significant reduction in RAM usage by dex file after layout.



image credit : Android Developers

3- Code Sinking

In this type of optimization instructions are moved closer to those instructions that actually use them. Instructions that are rarely used need to be moved closer to where they are used. It basically means instruction not in the regular flow should be moved to the exceptional cases.

code sinking

 image credit : Android Developers


4- Class Hierarchy Analysis

It is a common optimization technique in object oriented language a runtime will try to infer classes and methods that can be made final even though they aren’t final actually. Having this information internally gives a lot of room for the compiler for more optimizations.

class hierarchy

 image credit : Android Developers


5- Loop Optimization

Programs tend to spend most of the time inside loops. Loop optimization has an analysis part where we look into the program and transformations are performed in the optimization part.loop optimization

 image credit : Android Developers


6- Optimized Data Containers

Few classes provided are not optimized for usage on mobile devices. Implementation of generic HashMap is a bit memory intensive due to its requirement for a separate entry object in each mapping. This memory issue can be solved by using optimized data containers like SparseArray, SparseBooleanArray, and LongSparseArray. As SparseArray classes avoid the system requirement to autobox the key and sometimes the value thus, they are more efficient. When required switch to raw arrays for lean data structure.


7- Choose Data Types Wisely

Using primitive types will reduce the memory usage. An integer objects consume 4 times more memory than a primitive int. Similarly, a boolean object will occupy more memory than a  primitive boolean type. For Constants prefer static final variables as they consume half the memory in comparison to enums.


Hopefully this article will help you gaining in-depth insights about various memory optimization techniques for Android application. Remember all classes in Java including the anonymous inner classes that create an object and writes accessor methods internally consume around 500 bytes of code.


Being a big concern it’s the professional responsibility of an IT firm to ensure not only customized app on latest cutting edge technology to meet industry standards and make business objectives more than obvious but also to develop android apps that are optimized for minimum memory usage and a delightful user experience.

You may be interested in:

  1. iOS Application Battery Optimization Techniques to Minimize Burden on Battery
  2. Android App: Battery Optimization Best Practices for Zero Compromise on Performance