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.
I have fixed the bug in this way:
using Xamarin.Forms.GoogleMaps;
namespace MyApp.Custom
{
public class CustomGoogleMap:Map
{
}
}
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
Most helpful comment
I have fixed the bug in this way:
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