Bug 32898 - Memory leak when TabbedPage is popped out
Summary: Memory leak when TabbedPage is popped out
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Android ()
Version: 1.5.0
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Rui Marinho
URL:
Depends on:
Blocks:
 
Reported: 2015-08-09 18:20 UTC by Sergey Komisarchik
Modified: 2015-10-12 14:15 UTC (History)
2 users (show)

Tags: ac tabbedpage
Is this bug a regression?: ---
Last known good build:


Attachments
test case (901.66 KB, application/octet-stream)
2015-08-09 18:21 UTC, Sergey Komisarchik
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Sergey Komisarchik 2015-08-09 18:20:02 UTC
Tested in 1.4.4, 1.5.0-pre1

Steps to reproduce:
1. Push TabbedPage to the navigation stack
2. Pop TabbedPage from the navigation stack
3. Result: TabbedPage and its children still in memory

Complete example (shared project):

using System;
using System.Threading.Tasks;

using Xamarin.Forms;

public class App : Application
{
    public App()
    {
        this.MainPage = new NavigationPage(new Page1());
    }
}

public class Page1 : ContentPage
{
    public Page1()
    {
        var stack = new StackLayout() { VerticalOptions = LayoutOptions.Center };

        stack.Children.Add(new Label() { VerticalOptions = LayoutOptions.Center, XAlign = TextAlignment.Center, Text = "Page 1" });
        stack.Children.Add(new Button() { Text = "try fix", Command = new Command(this.FixMemoryLeak) });

        this.Content = stack;
    }

    private WeakReference page2Tracker;
    private WeakReference tabTracker;

    protected override async void OnAppearing()
    {
        base.OnAppearing();

        if (this.page2Tracker == null)
        {
            var page2 = new TabbedPage() { Children = { new ContentPage() { Title = "tab" } } };
            page2.Appearing += async delegate { await Task.Delay(1000); await page2.Navigation.PopModalAsync(); };

            this.page2Tracker = new WeakReference(page2, false);
            this.tabTracker = new WeakReference(page2.Children[0], false);

            await Task.Delay(1000);
            await this.Navigation.PushModalAsync(page2);

            this.StartTrackPage2();
        }
    }

    private void FixMemoryLeak()
    {
#if __ANDROID__
        ((Android.App.Activity)Forms.Context).ActionBar.RemoveAllTabs();
#endif
    }

    private async void StartTrackPage2()
    {
        while (true)
        {
            ((Label)((StackLayout)this.Content).Children[0]).Text =
                $"Page1. But Page2 IsAlive = {this.page2Tracker.IsAlive}, tab IsAlive = {this.tabTracker.IsAlive}";

            await Task.Delay(1000);
            GC.Collect();
        }
    }
}
Comment 1 Sergey Komisarchik 2015-08-09 18:21:32 UTC
Created attachment 12423 [details]
test case
Comment 2 Rui Marinho 2015-08-16 16:04:07 UTC
Should be fixed in 1.5.0-pre2
Comment 3 Rui Marinho 2015-08-21 19:00:44 UTC
Correction pushed to 1.5.1-pre1.