views:

489

answers:

3

I have a Service running in the same process as my Application.

Sometimes the Android OS decides to kill my service (probably due to low memory).

My question is: does my Application get killed along with the Service? or how does it work exactly?

Thanks!

+1  A: 

Processes are killed by the low memory killer, not Applications. So unless you do extra work to get your Service running in a different process, then your Activities are killed along with your Service.

The low memory killer doesn't try to destroy any objects in your process, although the activity manager may call onDestroy on Activity objects that are no longer needed. But that happens as part of the regular activity lifecycle, not due to low memory conditions.

(By the way, I'm unclear whether you mean "application" in general, or your object that extends Application, or whether you mean your Activities that show the UI.)

joeo
by application I mean my object that extends from Application
Erdal
A: 

An application is something that has a user interface and if you have included a service along with it, then definitely it will be killed since Applications are terminated once the cached application queue becomes full

so create Service separate from application or in other words create an other project for it :)

btw, i'm not an experienced Android developer but thats i think what i learned by watching the Android development life cycle videos by Google

KiNGPiN
Definitely don't create another project. Also don't have the service run in another process (via android:process) except under very special circumstances.
hackbod
yes, I am trying to keep my service in the same process. It's probably faster and easier for me to just call static methods than to implement aidl
Erdal
+6  A: 

First be sure to read: http://developer.android.com/guide/topics/fundamentals.html#proclife

The key to this is that on Android a process is just a container for code -- or specifically one or more components (activities, services, receivers, providers). By default all components in a .apk get their own, dedicated process, that they all run in together. This is almost always what you want.

When the user is directly interacting with a component of that process (that is an activity) Android will try very hard to keep that process running, and you won't see it killed except under extraordinary circumstances.

When the user is no longer directly interacting with the process, then it becomes expendable relative to other processes as described in the referenced documentation. That is, empty processes (no interesting components) will be killed before processes holding activities that the user had been using, which will be killed before processes with running services. So having a running service will tend to keep your process around at the expense of other processes.

At the same time, we need to deal well with more and more applications leaving services running, often indefinitely, and often with memory leaks. So has a service runs for an increasingly long time, Android will try less and less hard to keep its process going. Effectively this means moving it down to the background bucket until the out of memory killer takes it out. After that, if the service still wants to run, then a new process will be created for it to be restarted in.

The upshot is that for normal services that are running a long time, it is expected behavior that their process will get killed after a while. This doesn't need to stop the service; a service that wants to continue running will do so, it will just need to be instantiated in a new process.

Of course as long as the user is interacting with an activity in your process, the process won't be killed, since this pulls it to the foreground category regardless of what is going on with any services in it.

hackbod
thanks for your helpful answer!so to be a little more specific: I subclass from Application and in it I hold a reference to my Service object. When my service gets killed, how can I get a reference back to the new one? or does my Application object also die along and when it gets re-instantiated everything is the same as when I first start the app?
Erdal
When killing something for memory, the entire process (including the application object) is killed and no code executed in it at this point.If the service is just being destroyed because it no longer needs to run, its onDestroy() will be called and when the service is later needed a new instance is created and onCreate() called.Personally I generally recommend against using Application; it doesn't really give you anything over just using static singleton classes, and I think sets up expectations for people (w.r.t. there being a more traditional application model) that don't really hold.
hackbod
I see what you mean. The only problem I had, forcing me to use the Application class was that I didn't want to bind to the Service from all of my different Activities. I only call bind during my Application's onCreate method and then all other activities access the Service through my Application singleton. Would you suggest something better?
Erdal
Have your singleton take an Application object that it can bind with (or just the Context that is the Application).
hackbod