Bug 23807 - ListView Leaks Memory
Summary: ListView Leaks Memory
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.2.3
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: ---
Assignee: Eric Maupin
Depends on:
Reported: 2014-10-13 23:48 UTC by John Beans
Modified: 2015-06-30 15:53 UTC (History)
15 users (show)

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

Test case for ListView leaking memory. (6.51 KB, text/plain)
2015-02-03 00:00 UTC, Alan Tonisson
Sample for Original Post (Edited ToDo Sample) (190.25 KB, application/zip)
2015-02-12 14:19 UTC, Dominic N [MSFT]

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 John Beans 2014-10-13 23:48:44 UTC
In Xam.Forms iOS, when a ContentPage has a ListView on it the ListView will leak memory in two ways:
1. When cells are scrolled, the cells are not recycled. You can continually scroll up and down a long list and continually generate memory allocations that are never recovered, even with a subsequent GC.Collect().
2. When the page is popped, the ListView does not surrender its native UIKit resources

To reproduce this, open the ToDo sample. Add a few lines to the TodoItemDatabase.cs to pre-populate some rows:

        public TodoItemDatabase()
            database = new SQLiteConnection (DatabasePath);
            // create the tables

	    // Following lines added to pre-populate
            if (database.Table<TodoItem> ().Count () < 100) {
                TodoItem newItem = new TodoItem ();
                for (int i = 0; i < 100; i++) {
                    newItem.Name = "copy" + i.ToString ();
                    newItem.Notes = "notes";
                    database.Insert (newItem);
                database.Commit ();

Run the ToDo app in debug mode under Instruments. Swipe the list up and down and watch the Allocation grow. You can add a GC.Collect() to the OnAppearing event, then navigate to and from the page to stimulate collection. But it won't help.

If you would like to see the #2 type of loss, add a page BEFORE the list page, and navigate to and from the list page. The list (even if it is not scrolled) will not surrender the memory allocated to the UIViews and other resources that it uses when you pop the page and return to the prior page.

I discussed this with Jason, he tried it and agrees that this should be reported.
Comment 1 Udham Singh 2014-10-14 07:59:01 UTC
I have checked this issue with the help of instructions provided in bug description and observed that when we scroll up and down in a long list then instrument generate memory allocations that are continually increased and memory for scrolled cells are not recycled/released.

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

Environment Info : 

=== Xamarin Studio ===

Version 5.5 (build 227)
Installation UUID: ce927b2a-2c07-44c5-b186-09cfdafba6dc
	Mono 3.10.0 ((detached/47db868)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 310000019

=== Apple Developer Tools ===

Xcode 6.0.1 (6528)
Build 6A317

=== Xamarin.iOS ===

Version: (Business Edition)
Hash: 959c1e4
Build date: 2014-10-03 00:25:37-0400

=== Xamarin.Mac ===

Version: (Business Edition)

=== Xamarin.Android ===

Version: 4.18.0 (Business Edition)
Android SDK: /Users/xamarin76/Desktop/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)
		4.4.87 (API level 20)
		4.5    (API level 21)
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)

=== Build Information ===

Release ID: 505000227
Git revision: 7b721eeec7a2fa4c4f4de0ecd2aed4dc25edac95
Build date: 2014-10-02 15:53:38-04
Xamarin addins: 99ed56b428b31eba1efaace4d82188d6f334e6ca

=== Operating System ===

Mac OS X 10.9.4
Darwin Xamarin76s-Mac-mini.local 13.3.0 Darwin Kernel Version 13.3.0
    Tue Jun  3 21:27:35 PDT 2014
    root:xnu-2422.110.17~1/RELEASE_X86_64 x86_64
Comment 2 Brendan Zagaeski (Xamarin Team, assistant) 2015-01-07 11:34:34 UTC
With a little luck this might have been fixed recently by the fix for bug 25257 (that fix is scheduled to be released in Xamarin.Forms 1.3.2 later this month).
Comment 3 Cody Beyer (MSFT) 2015-01-12 23:45:16 UTC
It does not appear that the fix for bug 25257 resolves this issue
Comment 4 Alan Tonisson 2015-02-03 00:00:53 UTC
Created attachment 9614 [details]
Test case for ListView leaking memory.

The file contains three classes: TestPage a page to display a ListView, PresidentCell: a ViewCell to display items in the list and President: a data item to bind to PresidentCells.

This example is derived from the example at:
but without the images.

To test, display the page, and click the button repeatedly. After about 10-20 clicks (depending on available memory) using an Android 4.4.2 (Lvl 19) emulator it goes into a garbage collection frenzy. It loses 140 GREFs every two clicks of the button.

It makes no difference if the list is bound to an ObservableCollection or if you directly set the ItemsSource you lose some GREFS each time you remove an item from the ListView.

If no DataTemplate is used, I believe it does not leak.
Comment 6 Dominic N [MSFT] 2015-02-12 14:19:14 UTC
Created attachment 9799 [details]
Sample for Original Post (Edited ToDo Sample)

Shared project from the ToDo Forms Sample edited to reproduce the issue in the initial comment.
Comment 7 Alexey Kinev 2015-05-10 14:40:40 UTC
This bug is actually critical, as having these leaks application on Android device just crashes after scrolling list 10-20 items with images up and down for a couple of times. And without images it just takes longer, but crashes can't be avoided.

Thus said, Xamarin.Forms.ListView is totally broken on Android at the moment.
Comment 8 Alexey Kinev 2015-05-12 13:31:09 UTC
When using ImageCell, TextCell (or lightweight ViewCell?) it actually works without crashes for long time, so can't say it's totally broken!
Comment 9 Adam 2015-06-20 01:31:40 UTC
Can confirm that this occurs in Windows Phone in 1.4.3-pre3 as well.

More information on tests performed here: http://forums.xamarin.com/discussion/43787/outofmemory-with-images-on-windows-phone

As Alexey mentioned it is associated with a ViewCell in a ListView and from the comments above looks like it is happening on all platforms.

ImageCell or TextCell seems to be immune from this.
Comment 10 Jason Smith [MSFT] 2015-06-30 15:53:54 UTC
The issue this bug original speaks to is now fixed. If you still have an issue in the 1.4.3 final release please open a new bug. Memory leak bugs should be kept as separate as possible so we don't lose track of them and because they are so hard to distinguish from each other without really getting into the details of the reproduction.

So again, if you still have a reproduction with a memory leak against 1.4.3. Please file a new bug report.