Closed MeintrupStefan closed 9 months ago
Hi @MeintrupStefan, we recommend avoiding displaying multiple maps in a list, for this case consider using Mapbox Static Images API https://docs.mapbox.com/api/maps/static-images/. It is a service that can generate map images from your style on demand. Those images will look like an embedded map, barring interactivity.
This is exactly what I need! @evil159 You are my man!
However, there still exists a memory leak on iOS when I only display a singular map.
It behaves interesting. When the MapWidget is build for the first time, approximately 100MB of RAM will be occupied, but changing the location and then rebuilding barely consumes any more RAM. The problem is that if you leave the screen, then Flutter does not free the RAM that was used by the MapWidget completely.
The problem is that iOS never really kills the application, even when the user minimizes the application for a long time. Inevitably leads to the RAM to overflow over time.
Still, you helped me a lot @evil159, because our application actually does not need to interact with the map
Thanks @MeintrupStefan, thanks for pointing this out. Indeed, it seems the resources associated with map widget weren't released when map widget got removed from the widget tree, it is addressed in this PR https://github.com/mapbox/mapbox-maps-flutter/pull/383 and will be available as a part of 1.0.0-beta.3 release.
@evil159 Even though we will use static images from now on, I build from your branch to see if the issue is fixed.
The fix improved the memory leak a LOT! However, there still seems something off
Action/Ram usage | RAM |
---|---|
I open my app and show a Screen without MapWidget: | 290 MB |
I open a Screen that contains one Map Widget | 420 MB |
I close the Screen that contains the Map Widget | 350 MB |
I minimize the app | 340 MB |
I open the app again | 350 MB |
I open the screen with map widget | 420 MB |
I close the Screen that contains the Map Widget | 370 MB |
I open the Screen that contains the Map Widget | 420 MB |
Now, this seems like the problem is solved, because the max RAM usage is at 420 MB now. And baseline shifts to only 350 RAM usage
However, now I go back into the screen that contains a list of MapWidgets and therefore render multiple MapWidget (6)
Action/Ram usage | RAM |
---|---|
Open ListView with MapWidgets | 650 MB |
Close the Scree that contains the ListView | 500 MB |
Minimize the App | 450 MB |
Open the App again | 470 MB |
I would say that there still exists an issue when you have multiple screens that render MapWidgets since they seem to increase the RAM baseline continuously even though the MapWidgets are disposed
I am happy to test any changes that you make and report back
I tried the same with my app with images instead of MapWidgets and it seems like Flutter increases the RAM Baseline by use even when the widgets are disposed after. So this might just be normal Flutter behavior.
Before, I was able to crash the app with a couple of clicks. Now, I did not manage to do that even after opening many Screens that contain MapWidgets.
I would consider your PR a fix @evil159, but having MapWidgets in many different screens still seems to increase the RAM baseline continuously. However, I am not sure how realistic this is if you use static images when you can.
We experience a memory leak on iOS that eventually leads to the app crashing
Once we display a MapWidget, the Ram consumption increases by 100+ MB. However, this happens only on iOS and not on Android.
The problem is especially prominent, if we render multiple mapWidgets in a ListView. A screen that contains 6 mapWidgets increased the RAM usage by 300MB. Just a couple of clicks are able to crash the app completely because the RAM usage exceeds 2GB.
Widget we display:
System details:
Tested on iOS 17.2.1 mapbox_maps_flutter: ^0.5.1 flutter doctor -v: