Termination

Processes cannot live forever. They take up a chunk of RAM, for your classes and objects, and these mobile devices only have so much RAM to work with. Eventually, therefore, Android has to get rid of your process, to free up memory for other apps and their processes.

How long your process will stick around depends on a variety of factors, including:

Going back to the scenario from above, we have an application with a single activity launched from the launcher, where the user can return to the launcher either by pressing BACK or by pressing HOME. You might think that this makes no difference at all on when the process would be terminated, but that would be incorrect. Pressing HOME would keep the process around perhaps a bit longer than would pressing BACK.

Why?

When the user presses BACK, your one and only activity is destroyed. When the user presses HOME, your activity is not destroyed. Android will tend to keep processes around longer if they have active (i.e., not destroyed) components in them.

The key word there is “tend”.

There is an element of the Android OS called the “out-of-memory killer”. Its job is to ensure that there is a reasonable amount of free system RAM available at all times, so Android can fork new processes for apps when needed. When free system RAM drops below a certain level, the out-of-memory killer will terminate a process. The out-of-memory killer will tend to terminate empty processes more readily than it will terminate process with 1+ running components. However, the out-of-memory killer also takes into account things like:

However, in general, processes with active (not destroyed) components will stick around a bit longer than processes without such components.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.