Skip to content

An example of a .NET MAUI Memory Leak using MauiCALayer

Notifications You must be signed in to change notification settings

MarcelStommel/LayerLeak

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Layer Leak Example

Explanation

The project in this repo displays a Memory Leak in .NET MAUI where instances of MauiCALayer aren't properly disposed of. The leak originates from the IShape? _shape field inside MauiCALayer, because IShapes are also Views by default. As a View the shape also holds a referance to the current NameScope and through that to all x:Nameed UI Elements on the same or hihger level in teh UI Tree. My guess is that through this the MauiCALayer ends up in a referance cycle with objc objects from the x:Nameed UI Elements. I also tried to recrated the Issue with bindable properties, but wasn't able to.

The App

This App reproduces the issue by repeditly swapping the content of a ContentView to emulate navigation. On startup the App asks what method of leaking should be used, currently only a non-leaking reference implementation and the leaking through named elements is possible. Once selected the app starts swapping in a new view into the ContentView every 100ms while also updating the swap count and current memory usaged as reported by GC.GetTotalMemory(false). The Collect button calls GC.Collect() and GC.WaitForPendingFinalizers() to 'force' a gc run and the Back button just navigates back to the main page.

Conclusion

Running the App with the leaking through named elements example displayes a constantly growing memory usage that doesn't sink even after 'forcing' multiple gc runs. Even navigating back to the main page causes the leaked memory to reside. Doing a gc dump with dotnet-gcdump shows a lot of MauiCALayer refrencing Border UI Elements through RoundRectangle and it's NameScope.

About

An example of a .NET MAUI Memory Leak using MauiCALayer

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages