This is Xamarin's bug tracking system. For product support, please use the support links listed in your Xamarin Account.
Bug 43941 - Memory leak with ListView's RecycleElement on iOS
Summary: Memory leak with ListView's RecycleElement on iOS
Status: VERIFIED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms (show other bugs)
Version: 2.3.2
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-09-01 14:56 UTC by Falko Schindler
Modified: 2017-07-05 06:19 UTC (History)
11 users (show)

See Also:
Tags: ac iOS memory
Is this bug a regression?: ---
Last known good build:


Attachments
Simple test case (24.47 KB, application/zip)
2016-09-22 04:59 UTC, Falko Schindler
Details

Description Falko Schindler 2016-09-01 14:56:51 UTC
The following example app allows you to open a ListPage containing a ListView with the RecycleElement strategy. When opening this page repeatedly by pressing the "Push" button, we should see "Constructor" and "Destructor" printed on the command line. (The garbage collector is triggered right within the constructor.) But on iOS the destructor is never called when using the RecycleElement strategy.

The MainPage contains nothing but a button:

    MainPage = new NavigationPage(new ContentPage {
        Content = new Button {
            Text = "Push",
            Command = new Command(o => MainPage.Navigation.PushAsync(new ListPage())),
        },
    });

The ListPage contains only a ListView and logs the construction and destruction to the command line:

    public class ListPage : ContentPage
    {
        public ListPage()
        {
            Console.WriteLine("Constructor");
            GC.Collect();

            Title = nameof(ListPage);
            Content = new ListView(ListViewCachingStrategy.RecycleElement) { // NOTE: no destruction with RecycleElement strategy!
                ItemsSource = new List<string> { "A", "B", "C" },
            };
        }

        ~ListPage()
        {
            Console.WriteLine("Destructor");
        }
    }

I wasn't able to find a workaround except for switching the caching strategy to RetainElement.
Comment 1 Cliff Cawley 2016-09-08 22:59:43 UTC
I just found this exact same problem so I can confirm it happens for me as well. If I switch to RetainElement then the destructor is correctly called
Comment 2 Jeffrey Romine 2016-09-20 12:59:02 UTC
I have the same problem and noticed that the memory leaks when switching the content of MainPage.  If I set the Listviews to RetainElement the memory leak goes away.
Comment 3 Jeffrey Romine 2016-09-20 13:05:58 UTC
I can confirm this is not limited to iOS.  It also is leaking memory on Android as well.
Comment 4 Ashley Gazich 2016-09-21 21:57:12 UTC
Hi, thank you for filing this report! Could you please attach a minimized runnable test case exhibiting the memory leak for investigation?
Comment 5 Jeffrey Romine 2016-09-21 21:59:21 UTC
https://www.dropbox.com/s/u9okuibawtl8xdd/testingapp.zip?dl=0

This is an example of it happening when switching the MainPage from one page to another.
Comment 6 Falko Schindler 2016-09-22 04:59:40 UTC
Created attachment 17664 [details]
Simple test case
Comment 8 Rui Marinho 2016-10-04 11:30:25 UTC
Can see the issue still in 2.3.3
Comment 9 adrianknight89 2016-11-12 07:39:10 UTC
I'm able to reproduce this on iOS in RetainElement mode as well. The issue also exists when the list is empty.
Comment 10 adrianknight89 2016-11-12 19:46:05 UTC
See https://github.com/xamarin/Xamarin.Forms/pull/524
Comment 11 Rui Marinho 2017-01-26 14:30:33 UTC
Should be fixed in 2.3.5-pre1
Comment 12 Saurabh Paunikar 2017-07-05 06:19:05 UTC
Verified using Xamarin.form version = 2.3.5.256-pre5

ScreenCast link: https://www.screencast.com/t/cVy9vIoj

Note You need to log in before you can comment on or make changes to this bug.