mapbox-maps-android
mapbox-maps-android copied to clipboard
PointAnnotationManager is causing Mapbox SDK memory leak
Environment
- Android OS version: 11
- Devices affected: all
- Maps SDK Version: 10.5.0
Observed behavior and steps to reproduce
When adding a PointAnnotation on the map as shown below, Mapbox is giving this alert:
E/Mapbox: [maps-android\Mbgl-Style]: Mapbox SDK memory leak detected! Style object (accessing setStyleLayerProperty) should not be stored and used after MapView is destroyed or new style has been loaded.
This piece of code is generating this alert:
var symbolManagerNodes = mapView.annotations.createPointAnnotationManager()
symbolManagerNodes.create( PointAnnotationOptions() .withTextField("5") .withTextColor("#282626") .withTextSize(13.0) .withTextOffset(listOf(0.0, 0.15)) .withIconSize(1.05) .withIconImage("nodeSelected") .withPoint(Point.fromLngLat(3.25654, 51.25664)) .withData(JsonPrimitive(5)) )
Expected behavior
No risk for memory leak
Hi @nijs9, when do you create the PointAnnotationManager during view lifecycle?
@nijs9 are you still experiencing this issue?
Good afternoon, please tell me, is this bug fixed? He worries me
@nijs9 are you still experiencing this issue?
Good afternoon, please tell me, is this bug fixed? He worries me
@nijs9 are you still experiencing this issue?
Could you please elaborate if you're facing the same issue and if yes please share the reproducible code snippet? Also answering https://github.com/mapbox/mapbox-maps-android/issues/1429#issuecomment-1156562687 could be relevant.
P.S. This warning message does not actually indicate that the leak is happening, it's more about using cached instance of either Style
or MapboxMap
but if those local references are GC-d - no leak should happen; actual leak should be examined via e.g. Android Studio Profiler and depends on the way you build your code.
I made my own separate class (mediator) to control drawing on the map. The mediator accepts the MapView constructor and initializes the following code:
lineManager = MapView.annotations.createPolylineAnnotationManager()
filemanager = MapView.annotations.createPolygonAnnotationManager()
viewManager = MapView.viewAnnotationManager
In order for this mediator not to be recreated every time, I store the mediator link in the viewmodel. Important - I have provided methods for freeing the memory of Mediator.onDestroy()
in which I reset all references (including event listeners) and clear collections.
Questions:
- Will there be a leak?
- how to disable endless messages
Mapbox: [maps-android\Mbgl-Style]: Mapbox SDK memory leak detected! Style object
(they are output without stopping)
@madbulok answers:
- Android Studio has the profiler and it's the correct way to understand if there's a leak. Technically - no, I could imagine that in case of slightly different lifecycle in your
Mediator
comparing to the one we use in SDK - someStyle
methods could be called on 'outdated' style object (if you're loading the new style). - If you have those messages non-stop - most likely you're doing something wrong from your end. In any case I'm softing the wording and downgrading log error to warning in https://github.com/mapbox/mapbox-maps-android/pull/1691 to reduce possible confusion.
@nijs9 Closing this issue. If you continue to experience this behavior, please feel free to reopen.