Routing keeps all the ViewModels in memory #3524
-
I have created a WPF app using .NET 7 and ReactiveUI. At some point I noticed that every time I navigate between View Models the previous ones are not disposed and they are kept in memory (checked with dotMemory). The code I am using for the navigation is:
The _viewModelFactory is just a factory that creates a new instance of the provided View Model type through DI. I saw that in the RoutingState class an ObservableCollection is used to keep track of the View Models, but if I am not mistaken since the View Models are referenced in that collection they are not disposed by the GC which results in memory leaks. Do you think that is the reason for the leak? What can be done to avoid such memory leaks, while retaining the NavigationStack? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
If you are in a WPF app, the majority of your navigations should be NavigateAndReset, not Navigate. You should only Navigate when it's something you intend the user to back out of (i.e. there will be a GoBack call at some point). Since this idiom is very uncommon on desktop platforms, this shouldn't happen very often |
Beta Was this translation helpful? Give feedback.
If you are in a WPF app, the majority of your navigations should be NavigateAndReset, not Navigate. You should only Navigate when it's something you intend the user to back out of (i.e. there will be a GoBack call at some point). Since this idiom is very uncommon on desktop platforms, this shouldn't happen very often