Android Question [Solved] Memory problem with Google Maps

vfafou

Active Member
Licensed User
Hello!
My app uses Google Maps library, to eventually show a map with two markers, animated to bounds every time it appears.
My problem is that every time the map is shown and being animated, I have a memory increase by 45-70 MB that they don't be released after marker1.Remove, marker2.Remove and mfragment.Clear.
The result is that after some hours the app crashes with OutOfMemory!

UPDATE:
The memory is being increased even if I move the map manually!

What should I do?

The code I use:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
   Private GMapEx          As GoogleMapsExtras
  
End Sub
B4X:
private Sub loadMarkers(mMap As GoogleMap,mMapName As String)
 
    Private mOp As MarkerOptions
         
    mOp.Initialize
         
    Private bDs As BitmapDescriptor
    Private bDf As BitmapDescriptorFactory
    Private pin As Bitmap
 
    Private AR       As AndroidResources
    Private SPin  As BitmapDrawable
    Private CPin  As BitmapDrawable
    SPin = AR.GetApplicationDrawable("tcar")
    CPin = AR.GetApplicationDrawable("passpin")
 
    pin = CPin.Bitmap
    bDs = bDf.FromBitmap(pin)
    mOp.Icon(bDs)
    mOp.Anchor(mOp.GetAnchorU,mOp.GetAnchorV)
    mOp.Position2(Starter.routeLat,Starter.routeLon)
 
    Select mMapName
        Case "M"
            M2C = GMapEx.AddMarker(mMap, mOp)
        Case "L"
            M2L = GMapEx.AddMarker(mMap, mOp)
    End Select
 
    pin = SPin.Bitmap
    bDs = bDf.FromBitmap(pin)
    mOp.Icon(bDs)
    mOp.Anchor(mOp.GetAnchorU,mOp.GetAnchorV/2)
    mOp.Position2(Starter.gpslat,Starter.gpslon)
    Private CLoc As Location
    CLoc.Initialize2(Starter.routeLat,Starter.routeLon)
    mOp.Rotate(Starter.gpsLoc.BearingTo(CLoc))
 
    Select mMapName
        Case "M"
            M1C = GMapEx.AddMarker(mMap, mOp)
            setMMarkers
        Case "L"
            M1L = GMapEx.AddMarker(mMap, mOp)
            setLMarkers
    End Select
 
End Sub
Where setMMarkers (or setLMarkers) is:
B4X:
private Sub setMMarkers
    Private LatLongB As LatLngBoundsBuilder
    LatLongB.Initialize
    LatLongB.Include(M1C.Position)
    LatLongB.Include(M2C.Position)
 
    Private camFactory As CameraUpdateFactory
    Private camBounds As CameraUpdate
 
    camBounds = camFactory.NewLatLngBounds2(LatLongB.Build,pnlMapCms.GetView(0).Width,pnlMapCms.GetView(0).Height,90)
    GMapEx.AnimateCamera3(GCMap,camBounds,100,Null)
End Sub
And when I hide the map I do:
B4X:
private Sub removeMarkers(rGMapName As String)
    Select rGMapName
        Case "G"
            If Marker2.IsInitialized Then
                Marker2.Remove
                Marker2 = Null
            End If
        Case "M"
            GCMap.Clear
            If M1C.IsInitialized Then M1C.Remove
            M1C = Null
            If M2C.IsInitialized Then M2C.Remove
            M2C = Null
            If Marker2.IsInitialized Then Marker2.Remove
            Marker2 = Null
        Case "L"
            GLMap.Clear
            If M1L.IsInitialized Then M1L.Remove
            M1L = Null
            If M2L.IsInitialized Then M2L.Remove
            M2L = Null
            If Marker2.IsInitialized Then Marker2.Remove
            Marker2 = Null
    End Select
 
    Private r As Reflector
    r.RunStaticMethod("java.lang.System", "gc", Null, Null)
    r = Null
 
    Private markGEx As MarkerExtras
    markGEx.SetRotation(Marker1,0)
 
End Sub
Thank you in advance!
 
Last edited:

Erel

Administrator
Staff member
Licensed User
There is no point in setting local variables to Null. The references will be removed when the sub ends.

What do you mean with mfragment.Clear? Are you creating new maps?
 

vfafou

Active Member
Licensed User
Hello Erel,
Thank you for your response!
There is no point in setting local variables to Null. The references will be removed when the sub ends.
What do you mean with mfragment.Clear? Are you creating new maps?
All above are a try to see if memory will be released...
I don't create new maps!
 

Erel

Administrator
Staff member
Licensed User
Can you post the error message when it crashes?

Are you able to reproduce it with an activity and a map and nothing else?
 

vfafou

Active Member
Licensed User
I'll try it, but I may have to wait for 6-7 hours...
I'll make a small app with a map and do the job that my app does.
 

vfafou

Active Member
Licensed User
Hi Erel!
I've finally found that I wasn't calling my removeMarkers sub from one sub that I should be!
It still seems strange to me the fact that 2 markers can occupy up to 70 MB every time they are loaded!
Searching for the "problem" with the maps, I've found another maybe more important problem:
There is a worrying heap increase every time is being something to my app, for example loading a panel with 2 labels to the CustomListView takes about 1MB of RAM!
Just tell me if you want to open a new thread for this, to show you the results of memory analyser.
I think I'm doing something wrong but I can't figure out what!

Thank you in advance!
 
Top