Skip to main content

Android Activity causing Service crash due to heap fragmentation



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.


Comments

  1. 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.

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 1 Final Exam 2011 latest (hot hot hot)

  Hi! I have been posted content of ccna1 final exam (latest and only question.) I will post the answer and insert image on sunday. If you care, please subscribe your email an become a first person have full test content. Subcribe now  Some question  have not content because this question have images content. So that can you wait for me? SUNDAY 1. A user sees the command prompt: Router(config-if)# . What task can be performed at this mode? Reload the device. Perform basic tests. Configure individual interfaces. Configure individual terminal lines. 2. Refer to the exhibit. Host A attempts to establish a TCP/IP session with host C. During this attempt, a frame was captured with the source MAC address 0050.7320.D632 and the destination MAC address 0030.8517.44C4. The packet inside the captured frame has an IP source address 192.168.7.5, and the destination IP address is 192.168.219.24. At which point in the network was this packet captured? leaving host A leaving ATL leaving...