2009/11/20 Cédric Berger <[email protected]>

> But if your service process is killed due to low memory, won't the
> system try to restart it later ? (though of course if there are still
> too many services running there is still a problem)
>

Yes if the service was started with startService(), the system will continue
trying to restart it (using a back-off timeout to avoid thrashing too much)
until there is enough memory to run it again.  If there are just too many
services trying to run at the same time, you will end up cycling through the
services running each of them some of the time.

The only reason the service in this case would just be stopped and not
started again is if it has crashed a couple times, or has ANRed in the
background (in which case a dialog is not shown, but the system detects this
and kills the process).

Also note -- if you are using setForeground() it is EXTREMELY unlikely the
system is killing your service due to low memory, since this API basically
turns that off.  (Though note in 2.0 this API no longer does anything, and
you need to use the new APIs that tie this state to a notification; for
older releases, you are supposed to do this so the user can know what is
going on and stop the service, and this is now enforced in 2.0.)

If you find your service going away while it is in the foreground, you
should be looking for the service crashing or ANRing rather than thinking
the system is killing it for memory.

And I'll say again please please please avoid just running services
continually in the background.  We don't have enough memory in a lot of the
devices for more than a few applications to do this.  If you really need to,
please make it clear in your app that this is happening, with a UI for the
user to stop the service if they don't need it.  And you will just have to
deal with the fact that there will be situations where the system can't keep
your service running -- for example, if the user goes to a fairly
heavy-weight web site, this itself could consume enough memory that the
system needs to kill most if not all of the background services for that
time.

Finally, you can find some information about what the system is doing in the
log with "adb logcat".  There is often some more useful details in the
low-level event log that you can see with "adb logcat -b events".  If you
are going to ask for help with these kinds of questions, it would be
extremely useful to post the log output that is seen when the service
process goes away.

Also you can use "adb shell dumpsys activity.services" to see the state of
all services.  If your service has been started, even if its process is
killed and it is not running, you will see it in here, with a number of
useful things about its current state.

-- 
Dianne Hackborn
Android framework engineer
[email protected]

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to