The usefullness of task killer
apps is debated, but I'm wondering: how do they actually work? How is it possible to kill particular process?
Is there an API for this, and if so what does it actually do?
EDIT
Worth adding: I saw task killer apps kill processes on not rooted devices
. So, I wonder how is it possible to kill process, which you don't own in Android?
In a nutshell, Automatic Task Killers work by polling the OS for a list of currently running processes and the memory they are consuming. Then either with an intelligent algorithm or with user input the Task Killers issue a call to the system telling the system to kill the process. There are two apis you can do this.
They are
Process.killProcess(int pid)
ActivityManager.killBackgroundProcesses(String packageName)
This first works by invoking
Process.killProcess(int pid)
wherepid
is the unique identifier for a specific process. Android kills processes in the same way that linux does; however, a user may only kill processes that they own. In Android each app is run with a unique UID (UserID). Apps using this API an App can only kill their own processes, hence the following explanation in the docs forProcess.killProcess(int pid)
:When this method is called the signal is generated by the OS and sent to the process. Whenever a process receives a signal from the OS it must either handle that signal or immediately die. Signals such as
SIG_KILL
cannot be handled and result in the immediate death of the recipient process. If you want to kill processes that you don't have privileges to kill, i.e. its not your process, then you must switch users or escalate your privileges (on android this requires root privileges on the device).The second API works by telling the built in
ActivityManager
that you wan to kill processes associated with a specific Package. This API gets around the need for your UID to match the UID of the process because it requires the user to accept theKILL_BACKGROUND_PROCESSES
permission. This permission signals to the OS that an app has been approved by the user as a task killer. When a task killer wants to kill an app, it tells the OS to kill the process allowing an app to get around the problem of only being able to kill processes that it owns.In the Android Docs it says that this API actually uses the first
Process.killProcess
APIIf you want to know more I suggest you read about the Posix Signals and The Linux kill command