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.
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.
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.