Xamarin.forms.googlemaps: out of memory when use repeatedly Navigation.PushAsync

Created on 17 Mar 2018  路  7Comments  路  Source: amay077/Xamarin.Forms.GoogleMaps

When using a Navigation.PushAsync with a ContentPage that contains a map on Android and a button to navigate to a new page (of the same layout), navigating back and forth repeatedly will eventually cause an out of memory error.

VERSIONS

  • Xamarin.Forms.GoogleMaps - 2.3.0
  • Xamarin.Forms - 2.5.0.122203
  • *

PLATFORMS

  • [x] Android
  • [] iOS
  • [ ] UWP

HOW TO REPRODUCE

  1. create a page1 with a button
  2. create page 2 with a map
  3. navigate to page2 from page1 using Navigation.PushAsync (new page2()); in button click method
  4. go back to page1 using the navigation bar back button.
    5.- do step 3 until out of memory occurs
bug

Most helpful comment

I have fixed the bug in this way:

  1. I Create a folder named Custom in xamarin forms project and inside this class:
using Xamarin.Forms.GoogleMaps;
namespace MyApp.Custom
{
    public class CustomGoogleMap:Map
    {

    }
}
  1. In android project I added a folder named Renders and inside this class:
using Android.Gms.Maps;
using MyApp.Custom;
using MyApp.Droid.Renders;
using Xamarin.Forms;
using Xamarin.Forms.GoogleMaps.Android;
[assembly: ExportRenderer(typeof(CustomGoogleMap), typeof(GoogleMapRenderer))]
namespace MyApp.Droid.Renders
{
    public class GoogleMapRenderer : MapRenderer
    {
        protected override void Dispose(bool disposing)
        {
            //base.Dispose(disposing);
            (this.Control as MapView).OnPause();
            (this.Control as MapView).OnDestroy();
            (this.Control as MapView).Dispose();
        }
    }
}

3.- Then in my map xaml content page:

<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:MyApp.MarkupExtensions" xmlns:maps="clr-namespace:MyApp.Custom" x:Class="MyApp.Pages.MapPage"> <ContentPage.Content> <StackLayout> <maps:CustomGoogleMap x:Name="map" InitialCameraUpdate="19.397157, -99.139501, 13, 0, 0" VerticalOptions="FillAndExpand" /> </StackLayout> </ContentPage.Content> </ContentPage>

Notice that instead use :
xmlns:maps="clr-namespace:Xamarin.Forms.GoogleMaps;assembly=Xamarin.Forms.GoogleMaps"
I use:
xmlns:maps="clr-namespace:MyApp.Custom"

References:
https://bugzilla.xamarin.com/show_bug.cgi?id=39489

https://forums.xamarin.com/discussion/65294/memory-leak-in-the-xamarin-forms-maps-component-on-ios
comment wrote it by JGoldberger

All 7 comments

I have fixed the bug in this way:

  1. I Create a folder named Custom in xamarin forms project and inside this class:
using Xamarin.Forms.GoogleMaps;
namespace MyApp.Custom
{
    public class CustomGoogleMap:Map
    {

    }
}
  1. In android project I added a folder named Renders and inside this class:
using Android.Gms.Maps;
using MyApp.Custom;
using MyApp.Droid.Renders;
using Xamarin.Forms;
using Xamarin.Forms.GoogleMaps.Android;
[assembly: ExportRenderer(typeof(CustomGoogleMap), typeof(GoogleMapRenderer))]
namespace MyApp.Droid.Renders
{
    public class GoogleMapRenderer : MapRenderer
    {
        protected override void Dispose(bool disposing)
        {
            //base.Dispose(disposing);
            (this.Control as MapView).OnPause();
            (this.Control as MapView).OnDestroy();
            (this.Control as MapView).Dispose();
        }
    }
}

3.- Then in my map xaml content page:

<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:MyApp.MarkupExtensions" xmlns:maps="clr-namespace:MyApp.Custom" x:Class="MyApp.Pages.MapPage"> <ContentPage.Content> <StackLayout> <maps:CustomGoogleMap x:Name="map" InitialCameraUpdate="19.397157, -99.139501, 13, 0, 0" VerticalOptions="FillAndExpand" /> </StackLayout> </ContentPage.Content> </ContentPage>

Notice that instead use :
xmlns:maps="clr-namespace:Xamarin.Forms.GoogleMaps;assembly=Xamarin.Forms.GoogleMaps"
I use:
xmlns:maps="clr-namespace:MyApp.Custom"

References:
https://bugzilla.xamarin.com/show_bug.cgi?id=39489

https://forums.xamarin.com/discussion/65294/memory-leak-in-the-xamarin-forms-maps-component-on-ios
comment wrote it by JGoldberger

Same problem. Official xamarin maps docs says that:

MapView - The MapView is a specialized View subclass which can act as a host for a GoogleMap object. :exclamation: Users of this class must forward all of the Activity lifecycle methods to the MapView class :exclamation: .

https://docs.microsoft.com/en-us/xamarin/android/platform/maps-and-location/maps/maps-api#the-googlemap-class

A caso estas loco @_@ @josco007

Still have this problem with version 3.1.0.583944. I don't use maps. Only ListViews and other simple Controls.

Hi @MaxFe ,

I think it is not Xamarin.Forms.GoogleMaps's bug.

For shure not with the maps part of Xamarin.Forms. I only wrote here because we also experiencing memory leaks when navigating back and forth. Oh I overlooked that this is another package "amay077/Xamarin.Forms.GoogleMaps". Sry. I will have a look at the Xamarin.Forms issues

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JKennedy24 picture JKennedy24  路  6Comments

Gavrielo picture Gavrielo  路  3Comments

talhaa91 picture talhaa91  路  4Comments

amay077 picture amay077  路  4Comments

amay077 picture amay077  路  7Comments