2

I have an Android app (activity) which also has a corresponding service. The service is started by the activity and is supposed to run continuously even when the activity is stopped. When the activity is started again it can bind to the service and query it.

Sometimes the activity gets destroyed and created by the OS. This should not affect things, the activity should just be re-created and be able to bind to the service again. This basically works.

However...

I have found that both the Dalvik VM heap and the native heap are non-compacting and therefore constantly increase in size until the activity runs out of memory and crashes (even though the total memory usage is actually constant and not leaking). This is much exacerbated by destroying and re-creating the activity since a lot of allocations are done during the creation process.

This pretty much guarentees that the activity will crash after a number of restarts. This doesn't bother me that much, but what then happens is that the service also crashes because it is part of the same application. The service contains some important data which is then lost during the crash.

I am interested in any suggestions as to how to solve this problem?

Is there a way to separate the service from the activity (so that when the activity crashes it won't also crash the service), yet still have the service and the activity in the same application?

I could persist the service data but this would require many accesses to a DB and is not that conducive to saving battery.

trincot
  • 317,000
  • 35
  • 244
  • 286
chris
  • 1,731
  • 4
  • 26
  • 33

2 Answers2

0

Your should specify the service "process name" in the manifest, you can start the name with a ':' or a lower case character. to quote :http://developer.android.com/guide/topics/manifest/service-element.html

If the name assigned to this attribute begins with a colon (':'), a new process, private to the application, is created when it's needed and the service runs in that process. If the process name begins with a lowercase character, the service will run in a global process of that name, provided that it has permission to do so.

an example would be in the manifest

 <service android:name=".services.OfficeService"
            android:process=":myProcess" />
yehyatt
  • 2,295
  • 3
  • 28
  • 31
0

It sounds like your service is maintaining references to your defunct activities. You should be using a started service, not a bound service. See the guide topic on Services for details about the differences and how to use each one.

If you want to use a bound service (that is, you really need to use bindService() for some reason), then be sure to call unbindService() before your activity dies. Note that when the last activity unbinds, the service is halted.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • I need a service that is both started and bound, and I do actually call unbindService(). I think the problem is that because the service is still running (which is intended) then the current heap remains active. When the activity is restarted it uses the same heap as the service, which is by now seriously fragmented. – chris Feb 26 '12 at 23:40
  • 1
    @chris - You can test your hypothesis by specifying an [`android:process`](http://developer.android.com/guide/topics/manifest/service-element.html#proc) value for the service in your manifest. That will put the service in a separate process from your app's activities. – Ted Hopp Feb 26 '12 at 23:46