5

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?

Sandip Armal Patil
  • 6,241
  • 21
  • 93
  • 160
Android Developer
  • 559
  • 1
  • 5
  • 17
  • 3
    I think the question you should ask yourself is: Why do you *want* to kill your application? Android is really good at managing its foreground and background processes. It will kill processes when the user requests it or when it runs low on memory. – Cat Sep 10 '12 at 05:01
  • Actually I want to know about process management in Android Architecture. I just found this behaviour. Before checking it I believe that "every process has it's own process id and process gets killed as user come out of the application. Next time when relaunch it gets new process id." But now I find that "process keeps on running, even user come out of the application.And next time when you relaunch it gets the same process id." – Android Developer Sep 10 '12 at 05:20

2 Answers2

5

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.

Community
  • 1
  • 1
Cat
  • 66,919
  • 24
  • 133
  • 141
  • So, conversely, if background apps started dying without good reason (e.g. plenty of memory but Whatsapp gets closed) that would indicate a problem at the Android OS level? – jiggunjer Sep 06 '16 at 13:43
  • @jiggunjer I believe so, yes, but I can't say conclusively. – Cat Oct 25 '16 at 21:44
2

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.

Nikolay Elenkov
  • 52,576
  • 10
  • 84
  • 84
  • Yes I have already developed and published application without taking care of it. But I just found this behaviour. So I keen to know about it's affects or How android manages it? – Android Developer Sep 10 '12 at 05:12
  • This is somewhat more complex and the details are subject to change. Generally, Android won't kill a process unless it has to, because you are very likely to use an app you opened again after you used another app. Apps with an activity in the foreground have biggest priority, ones in the background and services have lower priorities. When memory is scares it starts killing processes starting from the ones with lowest priorities. It is all for your own (the users) good :) – Nikolay Elenkov Sep 10 '12 at 05:16