Android Memory Considerations For Developers

George Dellaratta -
2 MIN READ

Android devices are delivered with a fixed amount of random access memory (RAM) for use by the operating system and customer applications.  Understanding the amount of memory used by an application and how it is managed is important to assure efficient usage of the resource.

As applications instantiate objects, Android will allocate memory.  That memory will remain allocated to the application until the object is released and garbage collection can reclaim that memory.  As you can probably see, if the application continues to instantiate objects without freeing them, the application will continue to consume memory.  Refer to Overview of memory management  |  Android Developers for an explanation of how Android manages memory.

Manage your app's memory  |  Android Developers and Android Developers Blog: Avoiding memory leaks provides information for developers on how to write applications that use memory efficiently.  Android Studio 3.0 and later include a profiler that can be used to measure the amount of memory used by an application.

A close review of the Android activity life cycle will show that activities can get into the paused state and remain in this state.  This means that even if not in use, these activities will continue to consume memory.  Each activity runs inside of a process which is assigned to a prioritized predefined group of applications based upon the services that the process provides.  The foreground app has the highest priority and is in a group by itself.  Each group has an associated threshold of available memory.  A badly named (my opinion) feature of Android known as "Low Memory Killer" (LMK) will start forcing applications to close based on a last recently used algorithm as the threshold for that group is crossed.  Once the amount of available memory crosses back above the threshold, LMK stops forcing activities within that group to close.

For example, let's say that activites identified as content providers are grouped together with a threshold of 156MB.  If the amount of available RAM crosses below 156MB, then least recently used content providers will be forced closed.  The memory used by each is reclaimed until the amount of available RAM goes back above 156MB.

In summary, the amount of RAM in a mobile device is finite.  Applications (activities and services) should be written efficiently to assure stable operation of the device.  Google provides documentation (see above links and links within these links) and tools within Android Studio to help the developer assure their application doesn't leak memory and manages memory efficiently.

profile

George Dellaratta

Please register or login to post a reply

Replies