Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Xamarin.Forms memory leak when adding and removing ListView

$
0
0

The following code will generate memory leak when clicking the button several times:
`

public class App
{
    public static Page GetMainPage()
    {   
        var a = new string[]{ "a", "b", "c" };
        var lst = new ListView
        {
            ItemsSource = a,
            ItemTemplate = new DataTemplate(typeof(TextCell))
        };
        lst.ItemTemplate.SetBinding(TextCell.TextProperty, ".");

        var btn = new Button{ Text = "Click" };
        var stack = new StackLayout
        {
            Children =
            {
                new Label{ Text = "Hello, Forms!" },
                btn
            }
        };
        var lst1 = new Label{ Text = "haha" };
        btn.Clicked += (sender, e) =>
        {
            if (stack.Children.Count == 2)
                stack.Children.Add(lst); //Cause memory leak, but changing to lst1 won't leak
            else
                stack.Children.Remove(lst);
            GC.Collect();
        };

        return new ContentPage { Content = stack };
    }
}

`

Here is the output:

[Mono] GC_OLD_BRIDGE num-objects 15 num_hash_entries 17 sccs size 17 init 0.00ms df1 0.07ms sort 0.01ms dfs2 0.34ms setup-cb 0.01ms free-data 0.02ms links 2/2/2/1 dfs passes 34/19
[Mono] GC_MAJOR: (user request) pause 3.15ms, total 3.49ms, bridge 9.35ms major 704K/256K los 8K/8K
[Mono] GC_OLD_BRIDGE num-objects 25 num_hash_entries 27 sccs size 27 init 0.00ms df1 0.05ms sort 0.01ms dfs2 0.24ms setup-cb 0.01ms free-data 0.02ms links 2/2/2/1 dfs passes 54/29
[Mono] GC_MAJOR: (user request) pause 1.75ms, total 2.01ms, bridge 9.19ms major 720K/704K los 8K/8K
[Mono] GC_OLD_BRIDGE num-objects 43 num_hash_entries 49 sccs size 49 init 0.00ms df1 0.09ms sort 0.04ms dfs2 0.60ms setup-cb 0.39ms free-data 0.09ms links 15/15/15/1 dfs passes 107/64
[Mono] GC_MAJOR: (user request) pause 2.22ms, total 2.46ms, bridge 10.28ms major 720K/720K los 8K/8K
[Mono] GC_OLD_BRIDGE num-objects 53 num_hash_entries 59 sccs size 59 init 0.00ms df1 0.09ms sort 0.04ms dfs2 0.25ms setup-cb 0.03ms free-data 0.03ms links 15/15/15/1 dfs passes 127/74
[Mono] GC_MAJOR: (user request) pause 1.97ms, total 2.31ms, bridge 10.01ms major 720K/720K los 8K/8K
[Mono] GC_OLD_BRIDGE num-objects 71 num_hash_entries 81 sccs size 81 init 0.00ms df1 0.18ms sort 0.07ms dfs2 0.36ms setup-cb 0.04ms free-data 0.03ms links 31/31/31/3 dfs passes 183/112
[Mono] GC_MAJOR: (user request) pause 3.12ms, total 3.53ms, bridge 10.77ms major 768K/720K los 8K/8K

But if we add and remove lst1, then the leak won't occur.
Is it a bug?


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>