0

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.

Community
  • 1
  • 1
zIronManBox
  • 4,967
  • 6
  • 19
  • 35
  • If you are running out of memory, try to recycle the bitmaps by bitmap.recycle(). And use android:largeHeap="true" in your application tag in manifest. Check this and let me know. – Sripathi Jun 18 '14 at 05:18
  • I'm using `android:largeHeap="true"` already and bitmap.recycle() tried, and can't use in my case as its a drawable object that's created and returned instantly. (not under my control as API handles this) – zIronManBox Jun 18 '14 at 05:21
  • Are you getting outofmemory exception? – Sripathi Jun 18 '14 at 05:31
  • not yet, but GC_FOR_ALLOC triggers most of the times – zIronManBox Jun 18 '14 at 05:32
  • If you not getting outofmemory exception then how you can say that your running out of memory? If you are not getting outofmemory exception after GC_FOR_ALLOC triggered, it means you got some space in the stack to use. If you not having the required memory in stack then only you are running out of memory. – Sripathi Jun 18 '14 at 05:40
  • I'm not running out of memory, but it degrades the UI performance that what I meant to say due to `Grow heap` your UI basically hanges! – zIronManBox Jun 18 '14 at 05:42
  • Yes sure, and whats crazy is I'm also running to get the solution for the same problem. I'm trying for 3 more days.. Will come back if I get any solution, and don't forget to updated me if you find something.. :) – Sripathi Jun 18 '14 at 05:53
  • set null to imageview or mapview in on destroy method.your app will run smooth.as null automatically calls GC. – Pankaj Arora Jun 18 '14 at 06:16
  • onDestroy method of which one? and why would I set it to null, if I again have to reload the tiles back and forth? – zIronManBox Jun 18 '14 at 06:37
  • @DevCarlsberg Did try it, as mapview is bound by the library we can't directly set the complete view to null. And tile source cab be null, again to load the already loaded tiles would trigger GC_FOR_ALLOC – zIronManBox Jun 23 '14 at 10:56

2 Answers2

0

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.

kurtzmarc
  • 3,110
  • 1
  • 24
  • 40
  • Dear Sir, Please have a look at this link: http://stackoverflow.com/questions/23868345/optimize-android-code-snippet-better-design-approach?lq=1 – zIronManBox Jun 19 '14 at 10:10
  • Could you please direct me an example of usage of `inBitmap` withing the method I'm using in `public Drawable getDrawable(final InputStream aFileInputStream)` – zIronManBox Jun 19 '14 at 11:29
  • The rescale cache of maptileproviderbase takes about 15s: `I/org.osmdroid.tileprovider.MapTileProviderBase(22034): Finished rescale in 15814ms` Is this fine? BTW the zoom is from 16 to 17. Why does everytime rescale cache is triggered? Any work around? – zIronManBox Jun 23 '14 at 10:09
  • Could you please help me with usage of inBitmap? Feels like it may work. – zIronManBox Jun 23 '14 at 10:32
  • The rescale should not be taking that long, but that may be because of the memory pressure you are putting on the VM. Android has a good write-up on how to use inBitmap: https://developer.android.com/training/displaying-bitmaps/manage-memory.html – kurtzmarc Jun 23 '14 at 12:58
  • I did attempt to use `inBitmap` seems like maptiles are loaded and the latest one is drawn through the `mapView`. Could please help me with loading maptiles in the background using OSMdroid? Means, While zoom in or out, I don't maptiles to be loaded at the very instant, I can load it when mapView is untouched. – zIronManBox Jun 24 '14 at 04:42
0

This is listing different questions, which are in fact completely distinct.

  • issue about osmdroid memory consumption due to map tiles
  • question about how to receive network data in something looking like Push mode
  • issue about handling plenty of overlays => how much? (100?) Of which kind? Even not clear for me if this one is really an issue for you.

From my point of view, as it is, this question should be removed, to be replaced by specific questions, properly formulated.

MKer
  • 3,430
  • 1
  • 13
  • 18
  • Dear Mker, I'd posted very similar questions very specifically many times earlier by splitting all three things you've said. http://stackoverflow.com/questions/23508981/android-gc-for-alloc-freed-6346k-7-free-paused-143ms-total-143ms http://stackoverflow.com/questions/23868345/optimize-android-code-snippet-to-a-worker-thread-better-design-approach But no fruitful answers, so then I thought why not club as all these are related to each other. So, please don't mind my way of putting these question. If it offends then I would express my regret. – zIronManBox Jun 19 '14 at 03:41
  • Clarifying some of your doubts: 1.overlays are mostly 90% `ItemizedOverlay` and 10% `Polygon`. 2.please have a look at my other snippet of code at:http://stackoverflow.com/questions/23868345/optimize-android-code-snippet-better-design-approach 3.The network part I've mentioned in this question, which one would make sense `AsyncTask` or `Service`? – zIronManBox Jun 19 '14 at 03:53
  • Let's try to address issues in the right order. And first of all, you need a good basis: a map working with no issue (before handling plenty of overlays, and trying to implement advanced networking) => so I will put an answer (which will be mainly questions) to this post: http://stackoverflow.com/questions/23868345/optimize-android-code-snippet-better-design-approach – MKer Jun 19 '14 at 10:28