mapbox / mapbox/mapbox-maps-flutter

Memory leak if map is not visible

Open
#480 7 comments 0 reactions 0 assignees View on GitHub

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:
image

Not for the same run, but I assume the details are the same:
image

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),
            ]);
          }
        );
      })
    ]));
}

https://github.com/ristiisa/mbmemleak

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.