Android Process Killer

How do task killers work?

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) where pid 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 for Process.killProcess(int pid):

Kill the process with the given PID. Note that, though this API allows
us to request to kill any process based on its PID, the kernel will
still impose standard restrictions on which PIDs you are actually able
to kill. Typically this means only the process running the caller's
packages/application and any additional processes created by that app;
packages sharing a common UID will also be able to kill each other's
processes.

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 the KILL_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 API

Have the system immediately kill all background processes associated
with the given package. This is the same as the kernel killing those
processes to reclaim memory; the system will take care of restarting
these processes in the future as needed.

If you want to know more I suggest you read about the Posix Signals and The Linux kill command

How to kill currently running task in android

No one can kill process except Android OS itself.

Most of the task killer in android market don't kill the app they just restart the process

by using

public void restartPackage (String packageName)

when this method is called by your activity the operating system immediately called

savedInstanceState and save the state of that activity you want to kill. Now this process is

removed from memory and OS saved it state.Now when next time user start that activity it

will start from where it was killed or in other words restarted. You can verify it from any

task manager that they don't kill the process because no one can do so. This method also

work in ICS.

for above method you can look at here . As far as i know killBackgroundProcesses (String packageName) is for API 8 and above.

Android low memory killer & application back stack

When Android wants to reclaim resources (or just because Android likes to tidy up), it will usually just kill off any OS processes that are not hosting foreground activities or foreground services. Android does this by actually killing the OS process, therefore onDestroy() will NOT be called on any components that are active in those OS processes. You will not be warned about this, so there is no way to "catch" this behaviour.

How does process killing work in android?

Here is a wild guess from a novice:

Android sits on top of the linux operating system as shown here. If you connect to your android virtual device (avd) and open the terminal emulator or connect through the ADB, you will find that you have access to various linux commands.

C:\Users\james>adb -e shell
# cd /system/bin
# ls -a -l
...
lrwxr-xr-x root shell 2010-06-30 15:32 insmod -> toolbox
-rwxr-xr-x root shell 18172 2010-06-30 15:32 installd
lrwxr-xr-x root shell 2010-06-30 15:32 ioctl -> toolbox
lrwxr-xr-x root shell 2010-06-30 15:32 ionice -> toolbox
-rwxr-xr-x root shell 10036 2010-06-30 15:33 keystore
-rwxr-xr-x root shell 6520 2010-06-30 15:33 keystore_cli
lrwxr-xr-x root shell 2010-06-30 15:32 kill -> toolbox

Notice that most of the commands are just links to one small program (toolbox).
I can list the running programs with the ps command but if I try to kill them with the kill command... it says i do not have permission. You might be able to create a task manager that uses the underlying ps and kill commands in order to do the dirty work for you. You may have to overcome some permission issues though.

I expect you could run the system commands with something like this:

Runtime.getRuntime().exec("ls");

Edit:

I skimmed through this open source task manager but I didn't see how it goes about killing tasks. Maybe someone more experienced can explain it.

How do Task Managers kill apps?

You can send the signal using:

Process.sendSignal(pid, Process.SIGNAL_KILL);

To completely kill the process, it's recommended to call:

ActivityManager.killBackgroundProcesses(PackageName)

before sending the signal.

Killing an application, is Android going the right way in this process.?

Not only here, even with apps having Exit option, I have noticed that
clicking the exit button does not terminate it. So where is the
problem.?

If the application is designed properly, the exit option should kill it. But having an Exit option is frowned up in Android.

You can kill an activity using finish(). An application is a combination of activities, so if all activities are properly killed, the application wont be running in my understanding. I could be wrong though.

Long ago I read that there is no function provided in Android to kill
an app (don't know about current time). So shall the blame be given to
the OS.?

From what I know there is not an official to kill an application at once. But there are some hacks which allows you to kill all the activities at once.

Is there a proper way yet to close an app when we want and make it
behave normally.?

Well the following hack works well for me.

Close all the previous activities as follows:

Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("Exit me", true);
startActivity(intent);
finish();

Then in MainActivity onCreate() method add this to finish the MainActivity

setContentView(R.layout.main_layout);

if( getIntent().getBooleanExtra("Exit me", false)){
finish();
return; // add this to prevent from doing unnecessary stuffs
}

Android task killer

You can't kill other tasks that way because the Kernel will enforce permissions. Try this instead:

ActivityManager activityManager = (ActivityManager) context.getSystemService("activity");
activityManager.restartPackage(packageName);

you will need the following permissions in the manifest:

<uses-permission android:name="android.permission.RESTART_PACKAGES" />
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />

EDIT:

Actually restartPackage is deprecated. use killBackgroundProcesses() instead!



Related Topics



Leave a reply



Submit