Just like any other OS, Android (ART is responsible for this) uses memory mapping and paging for memory management . The internal storage for Android phones (64 GB, 128 GB etc ) is much bigger than the storage space available in RAM (2GB, 4GB,6GB,8GB etc). So entire contents of internal storage cannot be shifted to RAM at once. So, what the OS does is, it breaks the entire internal storage to blocks called pages and transfers only those pages from internal storage to RAM which are currently required by the app. Its just like you have an entire book with lot of information and you extract out only the required information in the form of notes that you will use for revision during exams. Each page is identified by what is called the Logical Address. A page in internal storage is analogous to a person living permanently in a town. A page has logical address in internal storage, a person has permanent address in town. On Android it is 4KB in size.

Now since a page would be transferred on RAM from internal storage when required by app, called paging in, it would have a new address called Physical Address. It is just like for job a person migrates from permanent address in hometown to a new city and now you have a new address your temporary address. The mapping between Logical address and Physical Address is called Memory Mapping. When the page is no more required the OS removes the page from RAM called paging out.
Any app will usually either use free memory to create new objects (dynamic memory allocation) or modify the existing memory mapped pages in RAM memory (now called dirty pages). Such memory remains resident in RAM and cannot be paged out. The only way to release memory from an app is to release object references that the app holds, making the memory available to the garbage collector or removing the memory mapped pages that are unmodified by app (called clean pages), which are also garbage collected. Garbage collection of memory is like recycling. Just like when you don’t need stuff you throw it in garbage. The municipality garbage collector recycles the stuff and makes it available for reuse to make new stuff .Similarly the memory garbage collector collects the memory no more required by app to be reused by app in future for further creation of objects
The heap which represents the part of RAM where dynamically allocated app objects are stored is further divided in to generations. The memory which is newly allocated sits in young generation area and the memory which was allocated long before sits in older generation or which is resident since very long is in permanent generation. Every generation has upper limits and when this limit reaches, Garbage collection happens.
Every app in Android usually executes in its own process and has a hard limit defined on how much dynamic memory it can use. In order to utilise memory efficiently if two or more apps want to read the same page, the Android OS shares the same RAM page between the two processes. If the app requests memory beyond the dynamic memory (heap) limit set for it, it crashes with OutOfMemoryError.

Now the RAM size is limited and the user may want to open a lot of apps. With launch of every app Android would generally create a process and when the user switches to another app, instead of killing it, the OS caches it so that if the user switches back to the same app it loads quickly. The OS can only accommodate so many apps in the RAM at a time. When the limit is reached the OS has a prioritized list according to which it starts killing apps that are cached. The apps inactive in background are killed first, then , last used apps, then Home app, then, Services, then perceptible apps like music player playing music in background, then the app current user is interacting with, then system services, system processes and at last native services.
Android Image Credits
Leave a Reply