Bug 54401 - Pages do not get garbage collected after popping them from the navigation stack
Summary: Pages do not get garbage collected after popping them from the navigation stack
Status: CONFIRMED
Alias: None
Product: Forms
Classification: Xamarin
Component: Android ()
Version: 2.3.5
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2017-04-03 12:39 UTC by Josha Munnik
Modified: 2017-07-12 22:45 UTC (History)
4 users (show)

Tags: ac android memoryleak navigation
Is this bug a regression?: ---
Last known good build:


Attachments
Test project using Debug.WriteLine within destructors. (188.42 KB, application/x-zip-compressed)
2017-04-03 12:39 UTC, Josha Munnik
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 for Bug 54401 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
CONFIRMED

Description Josha Munnik 2017-04-03 12:39:14 UTC
Created attachment 21149 [details]
Test project using Debug.WriteLine within destructors.

The latest instance of a page that contains one or more ToolbarItem entries in the ToolbarItems property does not get garbage collected after popping it from the navigation stack.

It will get garbage collected once a new instance of the *same* page type is pushed on the navigation stack.

Pages without ToolbarItem entries that are popped from the navigation stack are not garbage collected until a new page of any type is pushed onto the NavigationStack.

Tested with Android.
Comment 1 Jimmy [MSFT] 2017-04-03 15:02:53 UTC
Thank you for filing this! I will try to confirm the issue using the attached project and I will update the report.
Comment 2 Josha Munnik 2017-04-07 08:51:08 UTC
I solved this by adding the following handler to NavigationPage.Popped

    private void HandlePopped(object aSender, NavigationEventArgs anEvent) {
      foreach (ToolbarItem item in anEvent.Page.ToolbarItems) {
        item.Parent = null;
        // not doing this will not garbage collect the attached view model
        item.ClearValue(ToolbarItem.CommandProperty);
      }
    }

Pages and view models get garbage collected.
Comment 3 Josha Munnik 2017-04-07 08:53:14 UTC
Forgot to mention, above has been tested with Xamarin Forms v2.3.4.224
Comment 4 Jimmy [MSFT] 2017-06-05 17:31:31 UTC
I can confirm this issue using the attached repro project. On iOS, when a page is popped it is automatically destroyed. However this is not happening on Android. 

While you can manually force a garbage collection so the popped pages are destroyed, this presents some other issues as mentioned in the description:

1. If a Page has ToolbarItems, it will not be garbage collected unless there was another page instance of the _same_ type on the stack. This means that the previous instance of this Page does _not_ get collected while the older instances are.

2. If a Page does not have ToolbarItems, then the Page will be collected as long as it is not the previous one. 


### Version Tests
2.3.6.106   BAD
2.3.5-pre3  BAD
2.3.4.247   BAD