Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

google_maps_flutter Memory Leak when map is shown, memory allocated never dispose #35243

Closed
darkmarthur opened this issue Jun 28, 2019 · 48 comments
Assignees
Labels
a: annoyance Repeatedly frustrating issues with non-experimental functionality c: crash Stack traces logged to the console c: performance Relates to speed or footprint issues (see "perf:" labels) customer: crowd Affects or could affect many people, though not necessarily a specific customer. p: maps Google Maps plugin package flutter/packages repository. See also p: labels. perf: memory Performance issues related to memory platform-ios iOS applications specifically

Comments

@darkmarthur
Copy link

darkmarthur commented Jun 28, 2019

Steps to Reproduce

Problem:

I have been facing a critical issue with the GoogleMap plugin, the problem is on iOS devices, when the GoogleMap widget is RENDERED (please note that the problem is not when instantiating the object) it appends a lot of memory to the current app, when the map is disposed, that memory allocated never goes away, it keeps growing as the Map is called to be shown.

Why it is a critical issue:

This is a critical issue because on iOS when the app reaches approximately 1gb the OS closes the app unexpectedly, and its a blocker to launch our app to production.

Replicate:

Use the memory monitor of XCode, not the dart tools,
This is not replicable on the emulator, only on real iOS devices, you can use the google_maps_flutter example to test.

Request:

Please let us know if you can replicate the issue just to be sure that this is on your scope

Evidence:

Video from the google_maps_flutter example performance
https://drive.google.com/open?id=1c78VJhBYsKc8HRX-8_RiR56J1LHnoWvJ

Marios-MacBook-Air:example mario$ flutter doctor -v
[✓] Flutter (Channel stable, v1.5.4-hotfix.2, on Mac OS X 10.14.5 18F132, locale en-MX)
    • Flutter version 1.5.4-hotfix.2 at /Users/mario/Documents/flutter
    • Framework revision 7a4c33425d (8 weeks ago), 2019-04-29 11:05:24 -0700
    • Engine revision 52c7a1e849
    • Dart version 2.3.0 (build 2.3.0-dev.0.5 a1668566e5)

 
[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK at /Users/mario/Library/Android/sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-28, build-tools 28.0.3
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01)
    • All Android licenses accepted.

[✓] iOS toolchain - develop for iOS devices (Xcode 10.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 10.2, Build version 10E125
    • ios-deploy 1.9.4
    • CocoaPods version 1.6.1

[✓] Android Studio (version 3.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 34.0.1
    • Dart plugin version 182.5215
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01)

[✓] VS Code (version 1.35.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.1.0

[✓] Connected device (1 available)
    • iPhone 5s • 22C55890-748A-43E5-A991-563B622B2C39 • ios • com.apple.CoreSimulator.SimRuntime.iOS-12-2 (simulator)

• No issues found!
@tareq594
Copy link

have the same critical issue even with the emulator

@deven98
Copy link

deven98 commented Aug 8, 2019

Same issue with our app. RAM usage goes above a GB over time.

@DennisKragekjaer
Copy link

DennisKragekjaer commented Aug 11, 2019

Same issue here, while using the map memory just keeps rising, at some point the map freezes. I have been struggling with this, and it makes me doubt about the Flutter concept.

@eseidelGoogle eseidelGoogle added p: maps Google Maps plugin c: crash Stack traces logged to the console platform-ios iOS applications specifically labels Aug 12, 2019
@eseidelGoogle
Copy link
Contributor

@amirh would know how to route this.

@amirh amirh added the plugin label Aug 13, 2019
@amirh
Copy link
Contributor

amirh commented Aug 13, 2019

@iskakaushik

@iskakaushik iskakaushik self-assigned this Aug 13, 2019
@deven98
Copy link

deven98 commented Sep 1, 2019

Any rough ETA of a fix on this? This is main block for us from going into prod.

@filiph filiph added the a: annoyance Repeatedly frustrating issues with non-experimental functionality label Sep 11, 2019
@deven98
Copy link

deven98 commented Sep 14, 2019

Just as an addition, this seems to be on both platforms, not just iOS. For us, it is taking up more memory by the minute and then rebooting some Android phones completely, whereas iOS shuts the app down once it goes above a certain value. We rechecked for any recursive code that might be causing this same bug as well. We also removed maps to confirm and it does not seem to be happening anymore.

@eseidelGoogle
Copy link
Contributor

It looks like this is assigned, but not scheduled.

Do we have a reduction/example we could offer the team? Or does any usage of maps produce this? Thanks.

@deven98
Copy link

deven98 commented Oct 14, 2019

@eseidel Any implementation of maps causes this for us currently. Another flavor of the app without the map works perfectly without memory issues.

@eseidelGoogle
Copy link
Contributor

@iskakaushik has been busy with other work the last month or more. We should probably un-assign this and find a new owner.

@adar2378
Copy link

I'm also having the same problem, map never releases the allocated memory

@kattaliraees
Copy link

Any update on this memory leak issue for google map flutter plugin in iOS devices? This is affecting our production release plan

@server2245
Copy link

Same issue here.

@deven98
Copy link

deven98 commented Dec 3, 2019

To anyone who has this as blocker: I'd recommend temporarily replacing Google maps with the mapbox implementation - https://pub.dev/packages/flutter_map

@boominadhaprakash
Copy link

Facing same memory leak issue in iOS devices.

@boominadhaprakash
Copy link

To anyone who has this as blocker: I'd recommend temporarily replacing Google maps with the mapbox implementation - https://pub.dev/packages/flutter_map

Hi @deven98
Thanks for your suggestion. Will it support, Polylines, Markers, Circles for both iOS and Android?

@deven98
Copy link

deven98 commented Dec 8, 2019

@boominadhaprakash Tbh, we just needed markers for our app, but I did see code in the package for pretty much everything you mentioned, so do check it out. Solved our use cases.

@boominadhaprakash
Copy link

@deven98 Okay, thank you. I will check.

@s2bhilai
Copy link

is anyone working on this critical issue?

@adar2378
Copy link

I was also wondering if anyone's working on this? Our iOS app crashes after using for couple of minutes because of this memory leak

@Elvis5566
Copy link

same here...

@kattaliraees
Copy link

kattaliraees commented Jan 1, 2020

is this an issue with google maps plugin or flutter platform views? dealloc is getting called for the FLTGoogleMapController Obc class. I tried to clear map. Tried below but no luck

-(void)dealloc {

  NSLog(@"FLTGoogleMapController dealloc");
  [_mapView clear];
  _mapView.delegate = nil;
  _mapView = nil;
  _markersController = nil;
  _polygonsController = nil;
  _polylinesController = nil;
  _circlesController = nil;
  _registrar = nil;
}

Below code change in GoogleMapController.m reduced memory leak. we have 2 map screens, switching them 5-7 times made the app crash before, now it take 50+

GoogleMapController.zip

- (NSObject<FlutterPlatformView>*)createWithFrame:(CGRect)frame
                                   viewIdentifier:(int64_t)viewId
                                        arguments:(id _Nullable)args {

    static FLTGoogleMapController *fltVC = nil;

    if(fltVC) {
      [fltVC clearMapData];
      
      [fltVC addMarkersController:args];
      [fltVC addPolygonsController:args];
      [fltVC addPolylinesController:args];
      [fltVC addCirclesController:args];
    
    
    }
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        fltVC = [[FLTGoogleMapController alloc] initWithFrame:frame
                                        viewIdentifier:viewId
                                             arguments:args
                                             registrar:_registrar];
    });

  return fltVC;

  /*return [[FLTGoogleMapController alloc] initWithFrame:frame
                                        viewIdentifier:viewId
                                             arguments:args
                                             registrar:_registrar];*/
}

@kattaliraees
Copy link

@adar2378 Replace below files in the plugin folder
lib/src/controller.dart
lib/ios/Classes/GoogleMapController.h
lib/ios/Classes/GoogleMapController.m

GoogleMapController.zip

Added clearMapView(); API in controller.dart. Call it before you switch to different map view

@override
  void dispose() {

    mapController.clearMapView();
 
    super.dispose();
  }

@ricristian
Copy link

ricristian commented Jan 9, 2020

@kattaliraees is there any chance to add this fix directly in the package and the rest of users just update it directly from pubspec.yml ? Either the way many thanks for this solution
@eseidelGoogle

@kattaliraees
Copy link

@durduliu2009 we cannot consider this as an actual fix. This is a temporary workaround to reduce the quantity of memory getting leaked. Also not sure if this have any impact on anywhere else. But it works fine for our scenario. We are keeping an entire copy of plugin folder within our source code repository and referring to that in pubspec.yaml

  google_maps_flutter:
    path: ./lib/google_maps_flutter-0.5.21+11

@adar2378
Copy link

@adar2378 Replace below files in the plugin folder
lib/src/controller.dart
lib/ios/Classes/GoogleMapController.h
lib/ios/Classes/GoogleMapController.m

GoogleMapController.zip

Added clearMapView(); API in controller.dart. Call it before you switch to different map view

@override
  void dispose() {

    mapController.clearMapView();
 
    super.dispose();
  }

Thank you so much :)

@BorisLok
Copy link

BorisLok commented Jan 15, 2020

This change work for me. but it is not totally release all memory that map alloc, but it can release most.

Below code change in GoogleMapController.m

- (void) dealloc {
    NSLog(@"google map dealloc!!");
    [_mapView clear];
    [_mapView removeFromSuperview];
    _mapView.frame = CGRectZero;
    _mapView.delegate = nil;
    _mapView = nil;
    _markersController = nil;
    _polylinesController = nil;
    _polygonsController = nil;
    _circlesController = nil;
    _registrar = nil;
    [_channel setMethodCallHandler:nil];
    _channel = nil;
    _registrar = nil;
}

@ricristian
Copy link

ricristian commented Jan 18, 2020

@adar2378 Replace below files in the plugin folder
lib/src/controller.dart
lib/ios/Classes/GoogleMapController.h
lib/ios/Classes/GoogleMapController.m

GoogleMapController.zip

Added clearMapView(); API in controller.dart. Call it before you switch to different map view

@override
  void dispose() {

    mapController.clearMapView();
 
    super.dispose();
  }

I've tried it and I receive a message in debug console (tried meaning: copy google maps plugin into my local folder lib .. in that new folder from lib I've replaced files from the archive )

 Caught error: MissingPluginException(No implementation found for method map#clearMapView on channel plugins.flutter.io/google_maps_1)

image

pubspec.yml


dependencies:
  flutter:
    sdk: flutter

  flutter_localizations:
    sdk: flutter

  google_maps_flutter:
    path: ./lib/google_maps_flutter-0.5.21+15
  requests: ^2.0.1
  logger: ^0.7.0+2
  ....

@kattaliraees
Copy link

@durduliu2009 the clearMapView platform method is only added for iOS. So if you try to call it from the android build, it will give you the error. If the platform is android, don't call the clearMapView API. If you getting the error from the iOS build, try to clean and build and also make sure the files are replaced

@kattaliraees
Copy link

Also, remove the example folder from the plugin folder

@ricristian
Copy link

@durduliu2009 the clearMapView platform method is only added for iOS. So if you try to call it from the android build, it will give you the error. If the platform is android, don't call the clearMapView API. If you getting the error from the iOS build, try to clean and build and also make sure the files are replaced

I'm running this for android. So for android there is still memory leak and just for iOS exist that small workaround ? Many thanks in advance

@adar2378
Copy link

I didn't find the memory leak on android though. @durduliu2009

@cyanglaz cyanglaz added this to the June 2020 milestone Jan 22, 2020
@cyanglaz
Copy link
Contributor

flutter/engine#14326
flutter/engine#14275
These 2 PRs have been merged in recently which partially fix the issue. I tested with the Flutter on master and it seems the leak has reduced.

@cyanglaz
Copy link
Contributor

@kattaliraees @darkmarthur Do you still experience a huge amount of leaks using flutter on master?

@kattaliraees
Copy link

kattaliraees commented Jan 23, 2020

@cyanglaz switching between 2 maps screens 10 times in iOS Simulator. On-device it will crash once it crosses 1GB

Screen Shot 2020-01-23 at 11 54 21 PM

Screen Shot 2020-01-23 at 11 59 16 PM

@cyanglaz
Copy link
Contributor

@kattaliraees I also saw the big memory leak on simulator, but on real device, it seems to be better. Have you checked it on real devices?

@kattaliraees
Copy link

@cyanglaz Currently don't have a device with me. But will check and let you know soon

@kattaliraees
Copy link

@cyanglaz I can confirm that there is no memory leak on iOS Devices (verified on iPad 5th Gen iOS 13.3). It never crossed more than 450MB. Cheers 👏

@adar2378
Copy link

So we need to update to the latest version of flutter for this fix?

@kattaliraees
Copy link

@adar2378 The fix is not available in stable channel(which is the default). You have to switch to the master channel by running below

flutter channel master

@cyanglaz
Copy link
Contributor

@kattaliraees Thanks for confirming. I am going to close this issue since the original post is about leaking in the real device. @darkmarthur please reply if you found the issue not fixed and we can reopen and take a look again.

@liyuqian liyuqian added perf: memory Performance issues related to memory c: performance Relates to speed or footprint issues (see "perf:" labels) labels Jan 30, 2020
@harrypatel07
Copy link

@cyanglaz so do I need to change the channel from stable to master to resolved this memory issue?

@efraespada
Copy link

flutter channel master worked for me 👍

@lock
Copy link

lock bot commented Apr 25, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@lock lock bot locked and limited conversation to collaborators Apr 25, 2020
@flutter-triage-bot flutter-triage-bot bot added the package flutter/packages repository. See also p: labels. label Jul 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a: annoyance Repeatedly frustrating issues with non-experimental functionality c: crash Stack traces logged to the console c: performance Relates to speed or footprint issues (see "perf:" labels) customer: crowd Affects or could affect many people, though not necessarily a specific customer. p: maps Google Maps plugin package flutter/packages repository. See also p: labels. perf: memory Performance issues related to memory platform-ios iOS applications specifically
Projects
None yet
Development

No branches or pull requests