Each initialization of map leaks one Bitmap that contains sprite (icons). If user navigates a lot between app screen so that new map activities are created the leaks accumulate which eventually leads to the OutOfMemoryError or process kill.
In Crashlytics we see a lot of reports like this:
Fatal Exception: java.lang.OutOfMemoryError: Failed to allocate a 35346444 byte allocation with 16776472 free bytes and 25MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(VMRuntime.java)
at android.graphics.BitmapFactory.nativeDecodeByteArray(BitmapFactory.java)
at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:613)
at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:636)
Platform:
Android
Especially affects older Android versions:
Mapbox SDK version:
9.5.1
Problem description
Each initialization of map leaks one Bitmap that contains
sprite
(icons). If user navigates a lot between app screen so that new map activities are created the leaks accumulate which eventually leads to the OutOfMemoryError or process kill.In Crashlytics we see a lot of reports like this:
This call appears to be made from native code here https://github.com/mapbox/mapbox-gl-native/blob/master/platform/android/src/bitmap_factory.cpp
Steps to trigger behavior
MapboxAndroidDemo
appExpected behaviour
Memory usage doesn't grow on subsequent opening "A simple map view". The app doesn't crash after opening the screen many times.
Actual behavior
On each opening of "A simple map view" the memory usage grows by ~10MB. After repeating the open/close action multiple times the app gets killed.
Suggested fixes
Bitmap should be cached and reused to avoid overhead of large allocations. See https://developer.android.com/topic/performance/graphics/manage-memory Alternatively [Bitmap.recycle()](https://developer.android.com/reference/android/graphics/Bitmap?hl=en#recycle()) may be called explicitly to clear the resources.