CollectionView Windows Memory Leak, Android Laggy scrolling
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

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
- Run Monkey finder on Windows, in release build, link
- 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

Using this fix for Windows works fine
But reduces scrolling performance on Android, and may be causing memory leak (I can not check it for sure)
Relevant log output
No response
@jonathanpeppers
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.
I debugged this app quite a bit today, I believe there are two core problems here:
- Something XAML Hot Reload causes. Things worked better if I do:

I will investigate this further and let the right team know.
-
VisualElement.Backgroundsubscribes to events, which is a problem if theBackgroundcomes fromApp.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 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.
I was testing in Debug mode, so I could do things like put breakpoints in MAUI.
@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 in the example above, you could avoid using {StaticResource} including Style, Brush, etc. in a DataTemplate? Basically just hardcode values instead.