mapbox / mapbox/mapbox-maps-flutter
Memory leak if map is not visible
Nobody has claimed this yet.
- Dominant language
- Dart
- Stars
- 380
- Forks
- 204
- PR merge metrics
- No merged PRs in 30d
Description
It seems that if the map is in an IndexedStack and is not visible, then some kind of a condition is triggered that allows the memory usage to grow until no more memory can be allocated and the app crashes.
Over the course of 114s the app allocated 10GB of memory:
Not for the same run, but I assume the details are the same:
import 'package:flutter/material.dart';
import 'package:mapbox_maps_flutter/mapbox_maps_flutter.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
MapboxOptions.setAccessToken("<TOKEN HERE>");
runApp(MaterialApp(home: MapSample()));
}
class MapSample extends StatefulWidget {
@override
State<MapSample> createState() => _MapSampleState();
}
class _MapSampleState extends State<MapSample> {
final locationSettings = LocationComponentSettings(
enabled: true,
showAccuracyRing: true,
pulsingEnabled: false,
puckBearingEnabled: true,
puckBearing: PuckBearing.HEADING
);
var lastLocation = Position(0, 0);
Stream<Position>? location;
@override
initState() {
super.initState();
location = Stream.periodic(const Duration(seconds: 1), (_) {
lastLocation = Position((lastLocation.lng + 1) % 180, (lastLocation.lat + 1) % 90);
return lastLocation;
});
}
@override
Widget build(BuildContext context) => Scaffold(
body: IndexedStack(index: 0, children: [
Container(),
StreamBuilder<Position>(stream: location, builder: (context, snapshot) {
return MapWidget(
key: ValueKey(lastLocation.toJson().toString()),
cameraOptions: CameraOptions(center: Point(coordinates: snapshot.data ?? lastLocation), zoom: 4, pitch: 0.0),
onMapCreated: (MapboxMap mapboxMap) async {
await Future.wait([
mapboxMap.scaleBar.updateSettings(ScaleBarSettings(enabled: false)),
mapboxMap.gestures.updateSettings(GesturesSettings(rotateEnabled: false)),
mapboxMap.attribution.updateSettings(AttributionSettings(clickable: false, iconColor: 1)),
mapboxMap.location.updateSettings(locationSettings),
]);
}
);
})
]));
}
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 running the linked mbmemleak reproduction with the provided IndexedStack, StreamBuilder, and MapWidget setup, comparing memory use while the map is visible and hidden. Inspect the MapWidget lifecycle and location stream behavior during repeated updates; done means the hidden map no longer causes unbounded memory growth or an eventual allocation failure.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- dart, flutter
- Domain
- mobile
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100