mapbox-maps-android icon indicating copy to clipboard operation
mapbox-maps-android copied to clipboard

PointAnnotationManager is causing Mapbox SDK memory leak

Open nijs9 opened this issue 2 years ago • 6 comments

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

nijs9 avatar Jun 15 '22 12:06 nijs9

Hi @nijs9, when do you create the PointAnnotationManager during view lifecycle?

yunikkk avatar Jun 15 '22 14:06 yunikkk

@nijs9 are you still experiencing this issue?

ZiZasaurus avatar Jul 11 '22 13:07 ZiZasaurus

Good afternoon, please tell me, is this bug fixed? He worries me

@nijs9 are you still experiencing this issue?

madbulok avatar Sep 20 '22 09:09 madbulok

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.

kiryldz avatar Sep 20 '22 15:09 kiryldz

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:

  1. Will there be a leak?
  2. how to disable endless messages Mapbox: [maps-android\Mbgl-Style]: Mapbox SDK memory leak detected! Style object (they are output without stopping)

madbulok avatar Sep 25 '22 10:09 madbulok

@madbulok answers:

  1. 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 - some Style methods could be called on 'outdated' style object (if you're loading the new style).
  2. 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.

kiryldz avatar Sep 26 '22 12:09 kiryldz

@nijs9 Closing this issue. If you continue to experience this behavior, please feel free to reopen.

ZiZasaurus avatar Oct 24 '22 15:10 ZiZasaurus