maui icon indicating copy to clipboard operation
maui copied to clipboard

CollectionView Windows Memory Leak, Android Laggy scrolling

Open alfamizar opened this issue 3 years ago • 2 comments

Description

I noticed huge memory leak in my app, but seems this is widely spread issue, as it is easy to reproduce even in Monkey Finder https://github.com/dotnet-presentations/dotnet-maui-workshop.git

image

Also, scrolling performance is bad on Samsung Galaxy S20 Ultra (12GB RAM) if Data Tepmplate contains Frame, without wrapping with a Grid. (I mean fixing Memory leak on Windows, ruins scrolling performance (by potentional memory) on Android)

Steps to Reproduce

  1. Run Monkey finder on Windows, in release build, link
  2. Click Get Monkeys and see, how memory usage increases up to infinity

Link to public reproduction project repository

https://github.com/dotnet-presentations/dotnet-maui-workshop.git

Version with bug

7.0 (current)

Last version that worked well


Affected platforms

Android, Windows

Affected platform versions

Android 9, 12; Windows 11

Did you find any workaround?

Workaround for Windows memory leak is simple. Remove Grid, that wraps Frame image

Using this fix for Windows works fine image But reduces scrolling performance on Android, and may be causing memory leak (I can not check it for sure)

Relevant log output

No response

alfamizar avatar Feb 25 '23 10:02 alfamizar

@jonathanpeppers

rachelkang avatar Feb 28 '23 19:02 rachelkang

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.

ghost avatar Feb 28 '23 19:02 ghost

I debugged this app quite a bit today, I believe there are two core problems here:

  1. Something XAML Hot Reload causes. Things worked better if I do:

image

I will investigate this further and let the right team know.

  1. VisualElement.Background subscribes to events, which is a problem if the Background comes from App.Resources:

https://github.com/dotnet/maui/blob/7f3b34f53f629035fedb338d6f474a1cd019a858/src/Controls/src/Core/VisualElement.cs#L252-L265

Can be reproduced in a test:

[Theory]
[InlineData(typeof(ImmutableBrush), false)]
[InlineData(typeof(SolidColorBrush), false)]
[InlineData(typeof(LinearGradientBrush), true)]
[InlineData(typeof(RadialGradientBrush), true)]
public async Task BackgroundDoesNotLeak(Type type, bool defaultCtor)
{
	var brush = defaultCtor ?
		(Brush)Activator.CreateInstance(type) :
		(Brush)Activator.CreateInstance(type, Colors.CornflowerBlue);

	var reference = new WeakReference(new VisualElement { Background = brush });

	await Task.Yield();
	GC.Collect();
	GC.WaitForPendingFinalizers();

	Assert.False(reference.IsAlive, "VisualElement should not be alive!");
}

Only ImmutableBrush passes, as it is special cased and returns early.

I'll keep working on this. 👍

jonathanpeppers avatar Mar 02 '23 03:03 jonathanpeppers

@jonathanpeppers hi. Thanks for digging into it.

Regarding first problem, I am surprised, as I didn't expect that hot reload could cause issues in Release mode.

alfamizar avatar Mar 02 '23 06:03 alfamizar

I was testing in Debug mode, so I could do things like put breakpoints in MAUI.

jonathanpeppers avatar Mar 02 '23 12:03 jonathanpeppers

@jonathanpeppers if you find a workaround, that could fix an issue, before fix will be merged into .NET 7 MAUI, please, let me know.

alfamizar avatar Mar 02 '23 20:03 alfamizar

@alfamizar in the example above, you could avoid using {StaticResource} including Style, Brush, etc. in a DataTemplate? Basically just hardcode values instead.

jonathanpeppers avatar Mar 02 '23 21:03 jonathanpeppers