Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Application Vs. Process

I have created a Android Application with one Activity and package name "com.explore"

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

I run ps Command.

USER     PID   PPID  VSIZE  RSS     WCHAN    PC         NAME
app_137   2974  91    478604 48524 ffffffff 00000000 S com.explore

I press back button. I get out of the application and come to Home screen Now once again run ps command.

USER     PID   PPID  VSIZE  RSS     WCHAN    PC         NAME
app_137   2974  91    472428 42572 ffffffff 00000000 S com.explore

The process 2974 is still running. Can someone please explain me the behavior? That is memory usage,state or how long the process will persist? Should I kill process from my code after pressing back? How to kill process gracefully?

like image 651
Android Developer Avatar asked Sep 10 '12 04:09

Android Developer


2 Answers

When you run an application, if it is not running already, it gets a new process ID. This ID stays with it until it is no longer in memory, at all. (This can be achieved and tested by using the "Force Stop" option in Application Settings.)

However, when a user hits the back or home buttons, the app does not exit, and in most cases, only pauses or stops.

You can read this documentation, which details the process lifecycle in detail. Points 1 and 2 basically say that a foreground process (or one behind something superficial, such as a dialog) will not be terminated unless absolutely necessary (force close or no memory). Point 3 is important; it says that any process which is no longer necessary--i.e. the user has "closed" it--may or may not be killed, depending on the memory of the device. And lastly, point 4, anything that isn't doing anything, but is just.. existing.. will be killed as soon as possible.

You may also want to look at the Activity lifecycle. This will accurately show you when each of the steps of the application occur (that is, when it's paused, when it's stopped, and when it's destroyed). The only thing it doesn't really cover is over-allocation of memory (or orientation changes, which re-trigger the entire lifecycle).

To conclude, remember that Android is smart. It manages its processes better than most developers ever would, and does try to keep as many processes as possible for easy reuse. Unless you are absolutely certain that you want your process out of memory, you should never kill it. And the way we have to kill and manage processes is never pretty.

like image 111
Cat Avatar answered Sep 27 '22 16:09

Cat


Start reading the fundamentals. In short, in most cases you shouldn't be concerned with processes, Android takes care of starting and stopping those automatically. Killing process might result in the system restarting it, so there is usually not much to gain. Forget about 'task killers' and similar utilities they are nowadays mostly useless.

like image 26
Nikolay Elenkov Avatar answered Sep 27 '22 17:09

Nikolay Elenkov