Bug 41269 - Custom PageRenderers not getting released from memory
Summary: Custom PageRenderers not getting released from memory
Alias: None
Product: Forms
Classification: Xamarin
Component: iOS ()
Version: unspecified
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: E.Z. Hart [MSFT]
Depends on:
Reported: 2016-05-23 19:13 UTC by Rob Hedgpeth
Modified: 2017-07-07 03:45 UTC (History)
5 users (show)

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

Screenshots (200.63 KB, application/zip)
2016-05-23 19:13 UTC, Rob Hedgpeth

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:

Description Rob Hedgpeth 2016-05-23 19:13:39 UTC
Created attachment 16078 [details]

This was originally found within a client's app (2.0). I have also confirmed that it is occurring in the Xamarin custom page renderer sample project (2.2) - https://github.com/xamarin/xamarin-forms-samples/tree/master/CustomRenderers/ContentPage:

1.) Start profiling on the iOS project.
2.) Navigate from the MainPage to the CameraPage, and then press back (run through this process several of times for a more obvious example).
3.) After taking a snapshot with the Xamarin Profiler it's apparent that the CameraPageRender instances are not getting released from memory (see attached screenshots).
Comment 1 Slava Chernikoff 2016-10-24 10:23:46 UTC
+1 notices the same issue. Constructor of Page Renderer is always called but Destructor - never. Currently I just release used resources in Dispose.
Comment 2 E.Z. Hart [MSFT] 2017-07-07 03:41:58 UTC
The CameraPageRenderer *is* being collected. To demonstrate this, I've created a branch of the project at https://github.com/hartez/xamarin-forms-samples/tree/gc-demo. 

It's a slightly modified version of the original demo. Run it and tap the "Take Photos" button; after the camera interface loads up, hit the back button and watch the console output. You'll see the following lines:

   [0:] >>>>> GCCanary constructor
   [0:] >>>>> CameraPageRenderer iOS handle is 0; a handle of zero means the NSObject's link to this managed class has been released.
   [0:] >>>>> GCCanary finalizer

GCCanary is a small class which only has one reference - the CameraPageRenderer. When you see the finalizer for that class run, that means it's been garbage collected. If CameraPageRenderer's managed object were leaking, GCCanary would not be collectable. 

Also note that the CameraPageRenderer's Dispose method is being called, and after a short delay its Handle property goes to zero - this indicates that the iOS side has released the managed object. 

As Slava notes, if you add a finalizer to CameraPageRenderer it will never be called. This is as expected - Forms deterministically disposes of iOS objects (PageRenderer is a subclass of UIViewController), which means that the finalizer is suppressed. 

The profiler screenshots attached are from an older version of Forms; it's possible that there once was a bug which prevented custom page renderers from being collected and it's been fixed since. But without a full profiler session it's difficult to tell.
Comment 3 E.Z. Hart [MSFT] 2017-07-07 03:45:32 UTC
> hit the back button and watch the console output

Forgot to add that you'll have to hit the 'GC' button a couple of times to see the GCCanary class be collected.