Services and Timers

kolbe

Active Member
Licensed User
Longtime User
It appears that functionally if you create a service and within the service setup a timer to periodically do some work or if you periodically schedule the service to do the work, the result is the same.

My question is whether or not there is a resources reason to do one over the other. What if any is the difference when it comes to memory, cpu time, speed, etc?
 

thedesolatesoul

Expert
Licensed User
Longtime User
This is just my take on it.
Although, they are not the same, for executing a piece of code periodically, you can consider them similar.
The main difference comes when Android runs low on resources and decides to kill your service. Using StartServiceAt will allow your service to be invoked again, but if you use a timer, and your service gets killed, it will not start again, and moreover you will not know what state it died in.

Regarding resources:
Memory: This depends on what your service is doing. If it has a variable that holds a LARGE bitmap, then yes, when using a timer and keeping the service alive you are consuming too much memory. But when using StartServiceAt to schedule the service, if you schedule it too frequently you will be loading/unloading the bitmap too often.
CPU Time: I doubt there is much difference, as long as you are not wasting CPU cycles by idly calling a timer or service.


The short answer from me would be: If scheduling too frequent i.e. < 1 minute to 5 minutes a timer is better. If scheduling less frequent 1 hr + then probably services are better.
 
Upvote 0

kolbe

Active Member
Licensed User
Longtime User
This is just my take on it.
Although, they are not the same, for executing a piece of code periodically, you can consider them similar.
The main difference comes when Android runs low on resources and decides to kill your service. Using StartServiceAt will allow your service to be invoked again, but if you use a timer, and your service gets killed, it will not start again, and moreover you will not know what state it died in.

Can/Will Android just kill a service and not the whole process. In related thread, Erel made it clear that when Android decides to kill your process your scheduled service will be lost.

Also... when Android decides to kill, shouldn't you get the ServiceDestroy or ActivityPause event?
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Can/Will Android just kill a service and not the whole process. In related thread, Erel made it clear that when Android decides to kill your process your scheduled service will be lost.
Can you link me to that thread?

In this case, the service is the process. When Android clears your service out of memory, the scheduled service is not lost. It is removed from memory now, but it is scheduled, to run again later.
The same may or may not be the case in a service crash.

Also... when Android decides to kill, shouldn't you get the ServiceDestroy or ActivityPause event?
Yes, you will get the Service_Destroy event.
 
Upvote 0

kolbe

Active Member
Licensed User
Longtime User
http://www.b4x.com/forum/basic4android-updates-questions/16996-question-about-process-globals-services.html#post97156

Perhaps I need to better distinguish b/w crashing and android killing the process. If something crashes, be it activity or service, and an exception isn't caught, the whole process comes down and you loose the scheduling and globals, etc. If android kills it, then at least you will have the destroy/pause events.

I still get the impression that if the process is over, i.e. no more main activity, the scheduling of services are lost.
 
Upvote 0

corwin42

Expert
Licensed User
Longtime User
The short answer from me would be: If scheduling too frequent i.e. < 1 minute to 5 minutes a timer is better. If scheduling less frequent 1 hr + then probably services are better.

I think it is never a good idea to use a timer in a service module because you can't be shure that the timer really fires. Even if you set your timer to less than one minute your service process may be killed by the system and the timer event sub never gets called. You always should reschedule the service with StartServiceAt.

The only exception I can think of is if you use StartForeground. Then it will be safe to use a timer but then you have a notification icon, too.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
I still get the impression that if the process is over, i.e. no more main activity, the scheduling of services are lost.
The scheduling of service remains. I have been doing this for a while.

I think it is never a good idea to use a timer in a service module because you can't be shure that the timer really fires. Even if you set your timer to less than one minute your service process may be killed by the system and the timer event sub never gets called. You always should reschedule the service with StartServiceAt.

The only exception I can think of is if you use StartForeground. Then it will be safe to use a timer but then you have a notification icon, too.

I agree that a timer for scheduling constantly is not a good idea unless it is foreground. I was only thinking of short-term stuff, like download progress indication etc which is a situation in which the process cannot be killed.
 
Upvote 0

kolbe

Active Member
Licensed User
Longtime User
I've been doing both so... with this info I'll change the code to rescheduling and see if the results improve.

Thanks for the clarifications.
 
Upvote 0

mrjaw

Active Member
Licensed User
Longtime User
I am trying to do my own clock to check something but it needs to run even when the system is paused
any ideas?
 
Upvote 0
Top