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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.