Saturday, June 12, 2010
A lot of people have asked us what the best app is for killing tasks? Well, the answer is none of them. Sure there are some nice apps out there for killing tasks, but the fact is you just don’t need one with Android. In fact, most developers won’t even look at your logcat file if they see you running a task killer app on your Android-based phone.
To clear things up about this, Google’s Android developers blog has finally put this debate to rest about why a task killer is unnecessary, as well as why there are certain services that run in the background all the time, I’m sure at one time or another you’ve seen them and asked yourself “Why do those services keep starting after I kill them?”. Below you can read about when applications stop. If you want to learn more about this topic to can read the full post by clicking here.
When does an application “stop”?
A common misunderstanding about Android multitasking is the difference between a process and an application. In Android these are not tightly coupled entities: may seem present to the user without an actual process currently running the app; multiple applications may share processes, or one application may make use of multiple processes depending on its needs; the process(es) of an application may be kept around by Android even when that application is not actively doing something.
The fact that you can see an application’s process “running” does not mean the application is running or doing anything. It may simply be there because Android needed it at some point, and has decided that it would be best to keep it around in case it needs it again. Likewise, you may leave an application for a little bit and return to it from where you left off, and during that time Android may have needed to get rid of the process for other things.
A key to how Android handles applications in this way is that processes don’t shut down cleanly. When the user leaves an application, its process is kept around in the background, allowing it to continue working (for example downloading web pages) if needed, and come immediately to the foreground if the user returns to it. If a device never runs out of memory, then Android will keep all of these processes around, truly leaving all applications “running” all of the time.
Of course, there is a limited amount of memory, and to accommodate this Android must decide when to get rid of processes that are not needed. This leads to Android’s process lifecycle, the rules it uses to decide how important each process is and thus the next one that should be dropped. These rules are based on both how important a process is for the user’s current experience, as well as how long it has been since the process was last needed by the user.
Once Android determines that it needs to remove a process, it does this brutally, simply force-killing it. The kernel can then immediately reclaim all resources needed by the process, without relying on that application being well written and responsive to a polite request to exit. Allowing the kernel to immediately reclaim application resources makes it a lot easier to avoid serious out of memory situations.
If a user later returns to an application that’s been killed, Android needs a way to re-launch it in the same state as it was last seen, to preserve the “all applications are running all of the time” experience. This is done by keeping track of the parts of the application the user is aware of (the Activities), and re-starting them with information about the last state they were seen in. This last state is generated each time the user leaves that part of the application, not when it is killed, so that the kernel can later freely kill it without depending on the application to respond correctly at that point.
In some ways, Android’s process management can be seen as a form of swap space: application processes represent a certain amount of in-use memory; when memory is low, some processes can be killed (swapped out); when those processes are needed again, they can be re-started from their last saved state (swapped in).