mapbox / mapbox/mapbox-maps-android
MapView Memory leak
Nobody has claimed this yet.
- Dominant language
- Kotlin
- Stars
- 578
- Forks
- 161
- PR merge metrics
- No merged PRs in 30d
Description
## Environment
- Android OS version: Any
- Devices affected:
- Maps SDK Version: 10.16.0 and 10.10.1 (maybe others bellow it too)
## Observed behavior and steps to reproduce
A memory leak started to occur a while ago, it doesn't seem be related with any changes in our implementation but something internal in the SDK.
The leak happens every time we enter a screen that has the map view.
Also, we were only able to reproduce it when using `Don't keep activities` settings
```
┬───
│ GC Root: Global variable in native code
│
├─ com.mapbox.maps.NativeObserver instance
│ Leaking: UNKNOWN
│ Retaining 114.0 kB in 2977 objects
│ ↓ NativeObserver.onMapLoadErrorListeners
│ ~~~~~~~~~~~~~~~~~~~~~~~
├─ java.util.concurrent.CopyOnWriteArraySet instance
│ Leaking: UNKNOWN
│ Retaining 40 B in 4 objects
│ ↓ CopyOnWriteArraySet.al
│ ~~
├─ java.util.concurrent.CopyOnWriteArrayList instance
│ Leaking: UNKNOWN
│ Retaining 28 B in 3 objects
│ ↓ CopyOnWriteArrayList[0]
│ ~~~
├─ com.mapbox.maps.StyleObserver instance
│ Leaking: UNKNOWN
│ Retaining 113.4 kB in 2920 objects
│ ↓ StyleObserver.styleLoadedListener
│ ~~~~~~~~~~~~~~~~~~~
├─ com.mapbox.maps.MapboxMap$$ExternalSyntheticLambda1 instance
│ Leaking: UNKNOWN
│ Retaining 113.2 kB in 2914 objects
│ ↓ MapboxMap$$ExternalSyntheticLambda1.f$0
│ ~~~
├─ com.mapbox.maps.MapboxMap instance
│ Leaking: UNKNOWN
│ Retaining 113.2 kB in 2913 objects
│ ↓ MapboxMap.gesturesPlugin
│ ~~~~~~~~~~~~~~
├─ com.mapbox.maps.plugin.gestures.GesturesPluginImpl instance
│ Leaking: UNKNOWN
│ Retaining 3.5 kB in 118 objects
│ context instance of com.example.MainActivity with mDestroyed = false
│ ↓ GesturesPluginImpl.mapPluginProviderDelegate
│ ~~~~~~~~~~~~~~~~~~~~~~~~~
├─ com.mapbox.maps.MapController instance
│ Leaking: UNKNOWN
│ Retaining 105.1 kB in 2598 objects
│ ↓ MapController.pluginRegistry
│ ~~~~~~~~~~~~~~
├─ com.mapbox.maps.plugin.MapPluginRegistry instance
│ Leaking: UNKNOWN
│ Retaining 103.5 kB in 2556 objects
│ ↓ MapPluginRegistry.plugins
│ ~~~~~~~
├─ java.util.LinkedHashMap instance
│ Leaking: UNKNOWN
│ Retaining 608 B in 18 objects
│ ↓ LinkedHashMap["MAPBOX_ATTRIBUTION_PLUGIN_ID"]
│ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
├─ com.mapbox.maps.plugin.attribution.AttributionViewPlugin instance
│ Leaking: UNKNOWN
│ Retaining 94 B in 3 objects
│ ↓ AttributionViewPlugin.attributionView
│ ~~~~~~~~~~~~~~~
├─ com.mapbox.maps.plugin.attribution.AttributionViewImpl instance
│ Leaking: UNKNOWN
│ Retaining 2.7 kB in 58 objects
│ View not part of a window view hierarchy
│ View.mAttachInfo is null (view detached)
│ View.mWindowAttachCount = 0
│ mContext instance of com.example.MainActivity with mDestroyed = false
│ ↓ View.mParent
│ ~~~~~~~
├─ com.mapbox.maps.MapView instance
│ Leaking: UNKNOWN
│ Retaining 90.6 kB in 2261 objects
│ View not part of a window view hierarchy
│ View.mAttachInfo is null (view detached)
│ View.mID = R.id.map_view
│ View.mWindowAttachCount = 0
│ mContext instance of com.example.MainActivity with mDestroyed = false
│ ↓ View.mParent
│ ~~~~~~~
╰→ androidx.constraintlayout.widget.ConstraintLayout instance
Leaking: YES (ObjectWatcher was watching this because com.example.
MapboxFragment received Fragment#onDestroyView() callback (references to its views should be cleared to prevent
leaks))
Retaining 84.3 kB in 2155 objects
key =
watchDurationMillis = 5808
retainedDurationMillis = 808
View not part of a window view hierarchy
View.mAttachInfo is null (view detached)
View.mID = R.id.map_root_container
View.mWindowAttachCount = 0
mContext instance of com.example.MainActivity with mDestroyed = false
```
## Expected behavior
No memory leak.
## Notes / preliminary analysis
## Additional links and references
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the leak on Android with “Don’t keep activities” enabled, using the MapView and SDK versions listed. Inspect the NativeObserver listeners, StyleObserver, MapboxMap, plugin registry, and attribution view shown in the leak trace. Done means repeatedly entering and leaving the map screen no longer retains the destroyed activity or its views.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, kotlin
- Domain
- mobile-dev, performance
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100