Open RuddyOne opened 4 weeks ago
@AdamEssenmacher I have resolved this issue, although I'm not sure how. I moved around some of my code (custom navigation service) and its now disposing correctly.
Im happy for this to be closed if you cant think of a reason this may have happened int he first place.
Also a random one but would you be available for a chat about MAUI memory leaks? You seem to have a really good understanding of it and it would be great to get some knowledge form you if at all possible.
I am seeing some weird behaviour once I add the TearDownBehavior to my pages, they no longer dispose.
Just to be clear, there's a big difference between an object being disposed vs finalized. The GC will call finalizers automatically (unless GC.SuppressFinalize(Object)
has been used). Dispose()
is not automatically called by the GC.
This never gets hit
A tough thing about tracking memory leaks in .NET is that the GC doesn't behave deterministically. It's completely possible that your VM is eligible for collection (so not leaking) and still not be collected--even with explicit calls to GC.Collect()! To complicate things even more, the GC implementations are different for Android vs. iOS--neither use the standard .net core GC.
So, while you would be correct to conclude that your VM has leaked if its finalizer is never called, you need to be able to discern between 'never' and 'not yet'. The GarbageCollectionMonitor
class in this project solves this problem by making as many as 10 subsequent calls with a 200ms delay between them. There isn't any science behind this approach... It just seems to work consistently enough.
With all that in mind, if using TearDownBehavior
does actually cause a leak, I would be very interested in seeing a repro project or clear steps to reproduce. You can inspect the code at https://github.com/AdamEssenmacher/MemoryToolkit.Maui/blob/main/src/MemoryToolkit.Maui/TearDownBehavior.cs and see that the only references it holds are WeakReference
s and so shouldn't be causing a leak.
I have short 1:1 consultations available on my sponsor page. However, I think pretty much all I know about memory leak problems in MAUI is covered in the project readme and this discussion on MAUI's GH. Another good resource is this wiki article.
I am seeing some weird behaviour once I add the
TearDownBehavior
to my pages, they no longer dispose. If i remove theTearDownBehavior
, or set it to false the VM is correctly picked up by the GC.I am using DI on the page to set the BindingContext to the VM.
I am checking this by looking at the finalizer on the VM.
This never gets hit, this leads me to believe something in this project is holding a reference? I could be wrong as I only have a small understanding of these leaks in MAUI (I have been looking into leaks for the past week). I cannot share more code since this is a project for a company, if I get time I can try and replicate it on a new project.
Any idea whats going on here? Any help would be great.
Note: I think this project is great and honestly should give the MAUI team something to think about. Its a shame MAUI is expecting the developer to look after all of these memory issues (from my understanding).