Bug 60492 - Memory Leak when using Platform.CreateRenderer()
Summary: Memory Leak when using Platform.CreateRenderer()
Alias: None
Product: Forms
Classification: Xamarin
Component: iOS ()
Version: 2.4.0
Hardware: All All
: Normal major
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2017-11-02 12:54 UTC by Vesselin Georgiev
Modified: 2018-01-19 02:48 UTC (History)
4 users (show)

Tags: ios, createrenderer, memory leak, ac
Is this bug a regression?: ---
Last known good build:

A project that demonstrates the memory leak. (151.65 KB, application/zip)
2017-11-02 12:54 UTC, Vesselin Georgiev
Button Render is not released! (178.18 KB, application/zip)
2018-01-19 02:48 UTC, chung

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 60492 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:

Description Vesselin Georgiev 2017-11-02 12:54:58 UTC
Created attachment 25541 [details]
A project that demonstrates the memory leak.

We were able to identify a massive memory leak when calling Platform.CreateRenderer and Platform.SetRenderer. We track Xamarin Forms VisualElements and iOS renderers and both seem to leak consistently. Our exact case is a third party ListView control and is relatively complex, but we were able to reproduce the leak in a simple setup.
Our demo setup consists of creating a Xamarin Forms visual element (Grid in the demo project) and creating its renderer by calling Platform.CreateRenderer(). Later, we release the renderer by calling Platform.SetRenderer(grid, null).
We use WeakReference to track whether instances are alive, and we can see that they are never released.
This is a huge problem for us as we do similar operations for each cell in our ListView. Memory piles up quickly.
We observed this on both a simulator and a physical device.
We are attaching a zip containing two folders. One folder contains a runnable project and the other folder contains only the two files needed to reproduce the leak.
Comment 1 Paul DiPietro [MSFT] 2017-11-02 18:35:40 UTC
I will set this to confirmed until further investigation can be made.
Comment 2 chung 2018-01-18 09:17:34 UTC
I created a ListView Renderer by myself. and my page is leaked when calling Platform.CreateRenderer(). My problem like this completely. (using dataTemplate.CreateContent() to create ContentView and call Platform.CreateRenderer() to create renderer for ViewCell)

Please help me!
Comment 3 chung 2018-01-19 02:48:43 UTC
Created attachment 26097 [details]
Button Render is not released!

Replace Grid Element by Button. Renderer of button is not released that is cause.

ButtonRender.NativeView.PreferredFocusedView => ButtonRenderer;

I don't know how to remove reference of ButtonRender.NativeView.PreferredFocusedView. Please give me a idea?

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"

      <DataTemplate x:Key="dt1">
            <Button Text="button" />

    <Button x:Name="ButtonDoTest" Text="Do Test" Clicked="ButtonDoTest_Clicked" Margin="0, 50, 0, 0" />
    <Label x:Name="LabelInfo" />