Bug 23897 - ListView with static BindingContext leaks
Summary: ListView with static BindingContext leaks
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.3.0
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: ---
Assignee: Bugzilla
URL: https://github.com/xamarin/Duplo/issu...
Depends on:
Blocks:
 
Reported: 2014-10-17 09:57 UTC by Ludovic Thomas
Modified: 2014-11-10 13:16 UTC (History)
4 users (show)

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

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 FIXED

Description Ludovic Thomas 2014-10-17 09:57:05 UTC
*Overview*
When navigating a page off of a navigation stack, if the page contains a ListView with a static binding context, the memory of the page is not released.

*Steps to reproduce*
1) Create a Xamarin.Forms project and add the code from the following code:
(To facilitate comprehension, it's based on the memory page leaks code of https://bugzilla.xamarin.com/show_bug.cgi?id=21995, adding the list view to show the issue)

using System;
using Xamarin.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Collections.ObjectModel;

namespace Test
{
    public class App
    {
        private static List<WeakReference> pageRefs = new List<WeakReference>();
        private static FakeProvider fakeProvider = new FakeProvider();

        public static Page GetMainPage()
        {
            return new NavigationPage(CreateWeakReferencedPage());
        }

        private static Page CreateWeakReferencedPage()
        {
            GC.Collect();
            var result = CreatePage();
            pageRefs.Add(new WeakReference(result));

            return result;
        }

        private static Page CreatePage()
        {
            var page = new ContentPage();
            var contents = new StackLayout();

            contents.Children.Add(
                new Button
            {
                Text = "Next Page",
                Command = new Command(() => page.Navigation.PushAsync(CreateWeakReferencedPage()))
            });
            contents.Children.Add(
                new Label
            {
                Text = string.Format(
                    "References alive at time of creation: {0}",
                    pageRefs.Count(p => p.IsAlive)),
                HorizontalOptions = LayoutOptions.CenterAndExpand
            });

            var listView = new ListView
            {
                BindingContext = fakeProvider
            };
            listView.SetBinding(ListView.ItemsSourceProperty, "Items");
            contents.Children.Add(listView);

            page.Content = contents;
            return page;
        }
    }

    public class FakeProvider
    {
        public ObservableCollection<string> Items { get; private set; } 

        public FakeProvider()
        {
            Items = new ObservableCollection<string>();
            Items.Add("Item 1");
            Items.Add("Item 2");
            Items.Add("Item 3");
            Items.Add("Item 4");
        }
    }
}

2) Click the Next Page button and observe that a new page is created with a
fixed value label showing the number of page references alive. Select this
button a few times.

3) Select the Back button and then the Next Page button again.

*Expected results*
Reference count should reflect that the view is no longer in memory and set to
be GC'd.

*Actual results*
Reference count reflects that the view is still in memory. If you remove the list view from the view, it's working properly. And if you set a BindingContext to "new FakeProvider()" instead of the static variable "fakeProvider" it's working properly.

*Versioning information*
Xarmarin Forms Version: 1.2.3.6257
+ Reproduced also on 1.3.0.6262-pre0
Comment 1 Ram Chandra 2014-10-17 11:49:07 UTC
I have checked this issue and I am able to reproduce this issue.

When I run the above case I observed that view is not garbage collected. 

However, I tried the same to replace static variable "fakeProvider" with new FakeProvider() and remove the list view but I am still getting the same behavior.

I am getting same behavior on both android and mac solution.

Screencast: http://www.screencast.com/t/p95LyngK2

Environment Info

Xamarin.Forms: 1.2.3.6257

=== Xamarin Studio ===

Version 5.5.2 (build 3)
Installation UUID: 6ea47b0d-1852-4aaf-808d-373ff0a5002b
Runtime:
	Mono 3.10.0 ((detached/633e444)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 310000020

=== Apple Developer Tools ===

Xcode 6.0.1 (6528)
Build 6A317

=== Xamarin.Mac ===

Version: 1.9.0.530 (Business Edition)

=== Xamarin.Android ===

Version: 4.18.0 (Business Edition)
Android SDK: /Users/jatin66/Desktop/Backup/android-sdk-macosx
	Supported Android versions:
		1.6   (API level 4)
		2.1   (API level 7)
		2.2   (API level 8)
		2.3   (API level 10)
		3.0   (API level 11)
		3.1   (API level 12)
		3.2   (API level 13)
		4.0   (API level 14)
		4.0.3 (API level 15)
		4.1   (API level 16)
		4.2   (API level 17)
		4.3   (API level 18)
		4.4   (API level 19)
Java SDK: /usr
java version "1.7.0_65"
Java(TM) SE Runtime Environment (build 1.7.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

=== Xamarin.iOS ===

Version: 8.2.0.207 (Business Edition)
Hash: 3bf072d
Branch: 
Build date: 2014-10-15 21:44:26-0400

=== Build Information ===

Release ID: 505020003
Git revision: 6d4c806e383ba366b15fbccca9ddc1ba94f8d5ad
Build date: 2014-10-15 15:00:36-04
Xamarin addins: 069ddd29bb70a42238142eee9bac21a5e4b2f9f9

=== Operating System ===

Mac OS X 10.9.5
Darwin Jatin66s-iMac.local 13.4.0 Darwin Kernel Version 13.4.0
    Sun Aug 17 19:50:11 PDT 2014
    root:xnu-2422.115.4~1/RELEASE_X86_64 x86_64
Comment 2 Eric Maupin 2014-11-10 13:16:35 UTC
Fixed for next release.