Bug 45794 - ContentPages remain in memory from NavigationPage PushAsync and PopAsync
Summary: ContentPages remain in memory from NavigationPage PushAsync and PopAsync
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: iOS ()
Version: 2.3.3
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
: 45789 ()
Depends on:
Blocks:
 
Reported: 2016-10-21 17:21 UTC by John Miller [MSFT]
Modified: 2016-11-03 23:40 UTC (History)
2 users (show)

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


Attachments
Screenshot (73.85 KB, image/png)
2016-10-21 17:22 UTC, John Miller [MSFT]
Details
Sample Project (197.66 KB, application/zip)
2016-10-21 17:23 UTC, John Miller [MSFT]
Details
Sample Project (272.03 KB, application/zip)
2016-11-03 13:26 UTC, John Miller [MSFT]
Details
Repro with instance counters included (4.36 MB, application/x-zip-compressed)
2016-11-03 23:39 UTC, E.Z. Hart [MSFT]
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 ANSWERED

Description John Miller [MSFT] 2016-10-21 17:21:28 UTC
**Overview**:

NavigationPage leads to memory leaks from pages never being released, even after PopAsync. 

**Steps to Reproduce**:

1. Run the attached project in VS to an iOS Simulator. 
2. That will install the app on the simulator and confirm that it's deployed.
3. Stop debugging.
4. Open the Xamarin Profiler from the Analyze menu
5. Choose the Allocations template
6. Start recording
7. Filter allocations on "Test". This will show only instances of the Test classes in the sample (they are ContentPages)
8. Press the Go to Page 1 button to PushAsync a new page
9. Press the Go Back button to PopAsync

Continue this process and note the allocation count increasing. 

**Actual Results**:

Test2 instances keep increasing, despite them being popped. 

**Expected Results**:

Test2 should not keep increasing.

**Build Date & Platform**:

iOS 10
XF 2.3.2

**Additional Information**:

It was explained that Pages would remain in memory, even after PopAsync, for the duration of the NavigationPage. To test that, you can press the Reset Main Page button which does:

App.Current.MainPage = new NavigationPage(new Test1());
GC.Collect();

Even after this is done, allocations for Test1 increase and none of the Test2 allocations are freed. 

The attached screenshot shows my attempts at reproducing this buy following the information here. Note* Allocations increased up to 13 before I stopped testing. It seems nothing was ever freed.

I also noticed that Memory Allocation was increasing with the count increases. I thought maybe the profiler was just showing a total and not a working set, but the memory never dropped either so that does not seem to be the case.
Comment 1 John Miller [MSFT] 2016-10-21 17:21:40 UTC
Might be similar to: https://bugzilla.xamarin.com/show_bug.cgi?id=31171
Comment 2 John Miller [MSFT] 2016-10-21 17:22:54 UTC
Created attachment 18165 [details]
Screenshot
Comment 3 John Miller [MSFT] 2016-10-21 17:23:24 UTC
Created attachment 18166 [details]
Sample Project
Comment 4 Samantha Houts [MSFT] 2016-10-22 02:20:39 UTC
*** Bug 45789 has been marked as a duplicate of this bug. ***
Comment 5 E.Z. Hart [MSFT] 2016-11-02 21:31:07 UTC
John,

I tried to reproduce the issue using your repro project, but I just get a NullReferenceException as soon as I tap the "Go Back" button. Could you update your repro project?
Comment 6 John Miller [MSFT] 2016-11-03 13:26:45 UTC
Created attachment 18317 [details]
Sample Project

@EZ, uploaded a new attachment. Sorry about that...
Comment 7 John Miller [MSFT] 2016-11-03 13:27:01 UTC
Updating back to NEW per new sample project
Comment 8 E.Z. Hart [MSFT] 2016-11-03 23:37:14 UTC
John,

I think the profiler is showing you total number of allocations of those classes over the lifetime of the application, not the current number of instances in memory.
Comment 9 E.Z. Hart [MSFT] 2016-11-03 23:39:01 UTC
Created attachment 18323 [details]
Repro with instance counters included

I'm attaching a slightly modified version of the repro project which tracks the construction and destruction of the Test1 and Test2 pages. You can see that the number of instances does not grow as you push/pop instances of the pages.