views:

593

answers:

1

I have a mix-case IDL service that I'm using in 2 ways:

  1. The service will spawn a thread and make a network call to grab some XML content on Activity's behalf. The content is return back to the Activity through client's IDL that defines callback methods
  2. If user chooses notification option then service creates a Timer that gets executed repeatedly and creates a toolbar notification. It also cached the content so when Activity request an update it is served from the cache rather than another network call

So my questions are

  1. For scenario #1 what is the price (if any) I'm paying for using service for network calls instead of creating background thread directly in the Activity?
  2. For #2 - am I better off changing implementation to AlarmManager? I noticed that when I kill processes with TasKiller my service dies and never gets restarted, would AlarmManager-base job have better chance of recovery?
+1  A: 

For scenario #1 what is the price (if any) I'm paying for using service for network calls instead of creating background thread directly in the Activity?

I am assuming that, since you said this is an "IDL service", that this is what I call a remote service -- you are using AIDL to define an interface that is being used across process boundaries.

In that case, the cost is several MB of RAM for the second process, plus a bit of CPU time for the IPC overhead. How much that "bit of CPU time" is depends on how frequently it is called.

For #2 - am I better off changing implementation to AlarmManager?

Generally, yes. Ideally, services are in memory as little as possible.

I noticed that when I kill processes with TasKiller my service dies and never gets restarted, would AlarmManager-base job have better chance of recovery?

No, because "task killer" apps tend to abuse an API (in Dianne Hackborn's words) that will kill off everything, including scheduled alarms. At present, there is no reliable & efficient defense against "task killers" that I am aware of.

CommonsWare
Yes to AIDL. I understand the expense at the time of the actual call, I don't really have a choice for doing the on the separate thread since it's a network call that can take long time to complete. But what about in between the invocations? Is there any penalty for simply having such service doing nothing for long periods of time. Say I don't have notification and don't need to define Timer at all but still use the service to serve remote calls. Is this legit?
DroidIn.net
Sure, particularly if you use BIND_AUTO_CREATE on the client side. Then, Android will close down the remote service sometime after there are no bound clients. Ideally, again, you want to avoid having the service hanging around in memory all the time, just from the memory consumption angle.
CommonsWare
I'll take a chance to bore you with this a bit longer: Should I refactor my code in which I use the AIDL-exposed service in 2 ways1. Get XML from network on client behalf on demand2. Check for updates periodically, notify and cache the resultIt runs pretty snappy on my MyTouch but I would like to optimize it if I can
DroidIn.net
That optimization, if I understand what you want to do correctly, will not necessarily help your individual app's performance, but it will make it a nicer player on the device overall. "A dead service uses no battery", so to speak. So, if your refactoring means your service is out of memory except when it is doing meaningful work, and your polling period is reasonable (e.g., 10 minutes), the refactoring is probably a good idea.
CommonsWare
I just can't buy into using something that has "Alert" in it's name for my regular scheduled operations. Basically if I'm reading you right - services are poorly implemented and should be avoided even at expencse of "hacking" your code. It's just using AlertManager for cron op feels like a hack to me. Am I wrong? Is there a legit (recommended) scenario for AIDL service?
DroidIn.net
It's AlarmManager, not AlertManager, so your first concern seems unwarranted. Services are not "poorly implemented", but people seem to forget that these things are *phones*, not quad-core 4GB desktops hooked up to continuous AC power. For example, I may have to give Pandora the boot because its service chews up insane amounts of battery for no visibly good reason. Primarily, AIDL is for interoperability, providing APIs to third-party apps. I cannot think of a use for it solely within a single app.
CommonsWare
My bad - it is AlarmManager which I think sounds even more hacky than Alert, meaning each time I need an update I'm raising alarm. I do agree with obusing the services - I'm forced to use TasKiller even I hate the idea of selfmanaging my phone's resources. I do see the point though - I think what get's me is the "mentality of desktop" where you can sacrifice a bit of performance for abstraction sake.K - back to work
DroidIn.net