Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FlexLayout Memory leak #10904

Closed
xzidez opened this issue Oct 25, 2022 · 7 comments
Closed

FlexLayout Memory leak #10904

xzidez opened this issue Oct 25, 2022 · 7 comments
Labels
legacy-area-perf Startup / Runtime performance memory-leak 💦 Memory usage grows / objects live forever (sub: perf) platform/android 🤖 platform/windows 🪟 s/triaged Issue has been reviewed s/try-latest-version Please try to reproduce the potential issue on the latest public version s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working t/perf The issue affects performance (runtime speed, memory usage, startup time, etc.) (sub: perf)
Milestone

Comments

@xzidez
Copy link

xzidez commented Oct 25, 2022

Description

A flexlayout with ObservableCollection as BindableLayout.ItemsSource will never release any resources that has been in the collection.

Steps to Reproduce

Use public reproduction repo.
Start and click the button. See how instance counter keeps increasing and never decrease.

This is how the layout looks. Nothing strange just a flexlayout with a itemssource.

<FlexLayout WidthRequest="300" Direction="Column" BindableLayout.ItemsSource="{Binding Items}">
 <BindableLayout.ItemTemplate>
  <DataTemplate x:DataType="tmp:Item">
   <Label Text="{Binding Text}" HeightRequest="40" MinimumWidthRequest="100" BackgroundColor="LightGray"/>
  </DataTemplate>
 </BindableLayout.ItemTemplate>
</FlexLayout>

Viewmodel looks like this

public partial class ViewModel : ObservableObject
{
    [ObservableProperty]
    private ObservableCollection<Item> _items;

    public string InstanceCounterText => $"InstanceCount: {Item.InstanceCounter}";

    public ViewModel()
    {
        Items = new ObservableCollection<Item>();
    }

    public void Update()
    {
        Items.Add(new Item());
        if(Items.Count > 1)
        {
            Items.RemoveAt(0);
        }

        GC.Collect();
        OnPropertyChanged(nameof(Items));
        OnPropertyChanged(nameof(InstanceCounterText));
    }
}

And then the item in question

public class Item 
{
    public static int InstanceCounter = 0;
    public string Text { get; }
    public Item()
    {
        Text = $"Item number: {InstanceCounter++}";
    }
    ~Item()
    {
        InstanceCounter--;
    }
}

Link to public reproduction project repository

https://github.com/xzidez/MauiFlexLayoutLeak

Version with bug

6.0.400

Last version that worked well

Unknown/Other

Affected platforms

Android, Windows

Affected platform versions

Any Android or Windows version

Did you find any workaround?

No response

Relevant log output

No response

@xzidez xzidez added the t/bug Something isn't working label Oct 25, 2022
@PureWeen PureWeen added this to the Backlog milestone Oct 25, 2022
@ghost
Copy link

ghost commented Oct 25, 2022

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

@PureWeen PureWeen added the legacy-area-perf Startup / Runtime performance label Oct 25, 2022
@ggutschi
Copy link

ggutschi commented Nov 2, 2022

Also references to #9162 (comment)

@XamlTest
Copy link

XamlTest commented Jun 1, 2023

Verified this on Visual Studio Enterprise 17.7.0 Preview 1.0. Repro on Windows 11 and Android 13.0-API33 with provided Project:
10904.zip

Windows:
image

Android:
image

@XamlTest XamlTest added s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels Jun 1, 2023
@samhouts samhouts added the memory-leak 💦 Memory usage grows / objects live forever (sub: perf) label Aug 1, 2023
@jonathanpeppers
Copy link
Member

Is it possible this is solved by: #13550

Does this problem still occur on .NET 8?

@jonathanpeppers jonathanpeppers added the s/try-latest-version Please try to reproduce the potential issue on the latest public version label Sep 11, 2023
@ghost
Copy link

ghost commented Sep 11, 2023

Hi @xzidez. We have added the "s/try-latest-version" label to this issue, which indicates that we'd like you to try and reproduce this issue on the latest available public version. This can happen because we think that this issue was fixed in a version that has just been released, or the information provided by you indicates that you might be working with an older version.

You can install the latest version by installing the latest Visual Studio (Preview) with the .NET MAUI workload installed. If the issue still persists, please let us know with any additional details and ideally a reproduction project provided through a GitHub repository.

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@albyrock87
Copy link
Contributor

The problem is in your code.

This should be your Update method in order to trigger and wait for the GC correctly.

    public async Task Update()
    {
        Items.Add(new Item());
        if(Items.Count > 1)
        {
            Items.RemoveAt(0);
        }

        await Task.Yield();
        GC.Collect();
        GC.WaitForPendingFinalizers();
        OnPropertyChanged(nameof(Items));
        OnPropertyChanged(nameof(InstanceCounterText));
    }

image

That said, I think there are more complex use cases where BindableLayout leaks.
This is why I've opened #17290

@jonathanpeppers
Copy link
Member

I'm asking the MAUI team about #17290.

I don't think it actually solves a leak, but there may be some behavior around BindingContext that you're fixing that seems good. 👍

I do think we can close it, as you've shown in the screenshot. Thanks!

@ghost ghost locked as resolved and limited conversation to collaborators Oct 14, 2023
@Eilon Eilon added the t/perf The issue affects performance (runtime speed, memory usage, startup time, etc.) (sub: perf) label May 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
legacy-area-perf Startup / Runtime performance memory-leak 💦 Memory usage grows / objects live forever (sub: perf) platform/android 🤖 platform/windows 🪟 s/triaged Issue has been reviewed s/try-latest-version Please try to reproduce the potential issue on the latest public version s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working t/perf The issue affects performance (runtime speed, memory usage, startup time, etc.) (sub: perf)
Projects
None yet
Development

No branches or pull requests

8 participants