Android Memory Basics

Amit Gupta
2 min readApr 11, 2021

--

In this post , I just want to give basic understanding for those who just started the memory Profiling to optimise memory for Android.

Android kernel was first developed for Android Phone Memory and it was based on Linux-Kernel. The main difference between Android and all other Linux-Kernel based systems is that Android does not have a thing called “Swap space.”

Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. While swap space can help machines with a small amount of RAM, it should not be considered a replacement for more RAM. Swap space is located on hard drives, which have a slower access time than physical memory.

Memory management using Swap in/out

Android uses OOM Score to free the space.

goal is to free up memory by killing the processes based on their “visibility state” and amount of memory consumed.

Every process gave its oom_adj score by Activity Manager. It’s a combination of application state (e.g., foreground, background, background with service, etc.). Here is a short example of all oom_adj values:

# Define the oom_adj values for the classes of processes that can be
# killed by the kernel. These are used in ActivityManagerService.
setprop ro.FOREGROUND_APP_ADJ 0
setprop ro.VISIBLE_APP_ADJ 1
setprop ro.SECONDARY_SERVER_ADJ 2
setprop ro.BACKUP_APP_ADJ 2
setprop ro.HOME_APP_ADJ 4
setprop ro.HIDDEN_APP_MIN_ADJ 7
setprop ro.CONTENT_PROVIDER_ADJ 14
setprop ro.EMPTY_APP_ADJ 15

Higher values of omm_adj are more likely to be killed by the kernel’s oom killer. The current foreground app has an omm_adj of 0.

The OOM killer uses configurable rules based on free memory and omm_adj thresholds. ie, rules state “if free memory < X1, kill processes with omm_adj > Y1”

--

--