Search code examples
androidandroid-asynctaskosmdroidandroid-memoryandroid-intentservice

android mapview app runs into memory issues (Grow Heap): Help choose design AsyncTask or IntentService or Service?


I've a mapview which needs to display plenty of overlay from the network. Now my current design is using AsyncTask where in a socket connection is open in doInBackground() and in publishProgress() it displays the overlay on the mapview.

Now the problem with this design is:

  1. First there is overlays (ItemizedOverlays<Overlayitem>) about 100 or so which allocates some memory during the data processed. Next with overlay the map tiles has also to load, where in I run in memory problems of Grow Heap (frag case) as it has to load bitmaps (although I'm using AsyncTask to load and display in parallel executor).
  2. The panning and zooming will take long time as there are overlays displayed and same time map tiles has to be loaded. And sometimes ANR dialog pops up.
  3. Now my socket connection has to be open most of the time (say about 30mins) as I'm not sure at what time a new data is received, meaning the AsyncTask has to be running in the background for most of the time.

Now my plan is to move this AsyncTask class to IntentService class where this socket connection can be open most of the time, but how would I make it communicate with the FragmentActivity? Before moving to that, would it make sense to move to IntentServiceor Service at all from AsyncTask class that I'm already using?

Note: The only reason the I want to move to IntentService or Service is that I'm running into memory issues, and most of them by allocated memory to bitmap (maptiles). I tried cleaning the maptile cache as well, by not much luck. I tried calling System.gc() in a Timer thread to some extent it seems working, but long term not sure how it would perform.

EDIT: Bottom line is GC is causing my app to slow down. Also please have a look at this link, how I'm actually loading my maptiles: optimize android code snippet - better design approach? As a heads up, the maptiles loading is quite fast and good when no overlays are there. Its only when data is received through network, and tried to overlay on it, GC triggers multiple times.


Solution

  • Based on the comments it sounds like the GC is causing the app to slow down. How about instead of recycling your Bitmaps, you reuse them.