Android Question How to cleanly pause an activity containing a Google Map

JackKirk

Well-Known Member
Licensed User
Longtime User
What is the correct code to use in an Activity_Pause procedure to ensure a Google Map object in the activity is totally destroyed and any memory it was using is freed?

Thanks in anticipation...
 

JackKirk

Well-Known Member
Licensed User
Longtime User
Erel,

I seem to have a real weirdo.

I am developing what has turned into quite a major app - 11 modules and counting with (currently) 6 activities.

So it is not very easy to nett the problem down.

One of the activities uses Google Maps, another does a lot of 8MB bitmap manipulation.

I have a fairly reliable scenario to cause an out of memory error in the activity that does the bitmap stuff:

(1) enter the Google Maps activity
(2) whilst it is furiously adding tiles hit the back button
(3) do the bitmap activity a couple of times

If at step (2) I let Google Maps finish adding tiles then there are no problems.

I have been peppering my code with calls to this little gem:
B4X:
Public Sub Heap_stats
  
    Dim r As Reflector
    r.Target = r.RunStaticMethod("java.lang.Runtime", "getRuntime", Null, Null)
    Log("Maximum Allocated Memory = " & NumberFormat2(r.RunMethod("maxMemory")/(1024*1024), 1, 4, 4, False) & " MB")  
    Log("Current Allocated Memory = " & NumberFormat2(r.RunMethod("totalMemory")/(1024*1024), 1, 4, 4, False) & " MB")
    Log("Unused Allocated Memory = " & NumberFormat2(r.RunMethod("freeMemory")/(1024*1024), 1, 4, 4, False) & " MB")
    Log(".: Used Allocated Memory = " & NumberFormat2((r.RunMethod("totalMemory") - r.RunMethod("freeMemory"))/(1024*1024), 1, 4, 4, False) & " MB")
    Log(" ")
  
End Sub

in an attempt to work out what is going on with memory but without much luck.

If I insert this in the manifest it seems to go away:
B4X:
SetApplicationAttribute(android:largeHeap,"true")
but this seems to me to be a crude response - I would much prefer to understand what is going on - it might also not work on other device configurations.

Any thoughts...
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
What exactly are you doing?
I was afraid you would ask this - it is difficult to explain without giving an essay on what the app will do when finished.

I don't think the problem is in the bitmap manipulating activity - even though it manifests there.

The reason I say this is that you can hammer it many times without it falling over - it only falls over with out of memory if I do the scenario in my post #3.

In the meantime I have come up with a much more sophisticated response to hitting the Back button in the Google Map activity - I now capture the Back button tap and only allow the activity to finish when Google Map has settled (as indicated by callbacks).

I seem to be able to still generate the problem though.

Is there anything aside from finishing the activity that will free Google Map resources?
 
Upvote 0

leongcc

Member
Licensed User
Longtime User
If you are using Google Map API, maybe consider going back to accessing Google Maps with http+webview.
Complicated and custom Google Map app (not using API) can switch in and out of context reasonably quick.
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Try to call Map.Clear in Activity_Pause (assuming that the Ready event has fired).

http://stackoverflow.com/questions/...fragment-memory-leak#comment20254946_14523949
Erel,

I am already calling .Clear

The stackoverflow post you point to smells suspiciously like what I am experiencing - although a post further down says that particular problem was fixed some time ago.

If you are using Google Map API, maybe consider going back to accessing Google Maps with http+webview.
Complicated and custom Google Map app (not using API) can switch in and out of context reasonably quick.

leongcc,

Thanks for your interest in my problem.

I don't really understand "consider going back to accessing Google Maps with http+webview" - I presume this was a means of accessing Google Maps via B4A before the Google Maps library.

I have over 2000 lines of fully documented B4A code invested in the activity in question - might be a bit early to start taking major detours - a more likely route if all else fails is just see how it flies with the large heap alternative of post #3.

Thanks again...
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
After some considerable playing around with this, reading up on garbage collectors etc, googling for "google maps out of memory" etc:

My scenario is actually simpler:

(1) enter the Google Maps activity
(2) exit this activity with the back button
(3) do the bitmap activity a couple of times

I have a sneaking suspicion that what I am seeing is that google maps is somehow corrupting the garbage collector's calculation of when it needs to collect.

The only thing I can think of is maybe the google map tiles are somehow not being cleared when I exit the google maps activity.

I have been trying to work out if/how GoogleMapsExtras.TileOverlay.ClearTileCache mentioned in this post:

https://www.b4x.com/android/forum/threads/google-map-library-tile-overlay.25246/#post-147343

might be used to force the tile cache to be cleared - but I am hopelessly out of my depth so any help in this area would be greatly appreciated - including telling me I'm going up a dead end.

Thanks again...
 
Upvote 0
Top