DataGrid Memory Leak
See original GitHub issueDescribe the bug
DataGrid
keeps a NotifyCollectionChangedEventHandler
open on Items
when Items
is bound to a view model even after its removed from logic/visual tree. Memory leak happens when view model is not eligible to be garbage collected. I have a singleton view model that I bind to a new view instance every time the user navigates to the page.
Because of this open handle, DataGrid
is never disposed, causing thousands of Shapes.Rectangle
instances to survive GC.
To Reproduce
- Create a view model with an
ObservableCollection
of any type, populate it with a generous amount of items upon view model construction - Create instance of view model that is referenced as a singleton (making it ineligible for garbage collection)
- Create a view with
DataGrid
, bind Items to view model’sObservableCollection
, create dummy columns so that rows will be rendered. - Create presentation that instantiates a new view for every navigation,
DataContext
being set to the same view model - Navigate between view in question and elsewhere, thereby causing a new view to be instantiated and bound to view model. Repeat this a few times.
- Observe memory leak
Expected behavior Memory leak does not occur.
Screenshots
Screenshot of Instance Retention Graph created using Redgate Ants Profiler. This graph indicates surviving objects AFTER the view has been destroyed and successfully GC’d. Note WindchimeDataGrid
is my own inheritance of DataGrid
, however it only contains 10 lines of code to modify selection behavior and I’ve recreated this bug with the normal DataGrid
.
Also note my view model at the top, which is referenced ultimately as a single instance in my DI Container.
Desktop : Windows 10,0.19044
Issue Analytics
- State:
- Created 10 months ago
- Comments:8 (5 by maintainers)
Top GitHub Comments
@timunie Thanks for asking! I’m all set, setting the collection to null fixes the issue. I accidently closed with comment, I’ll let you guys do that.
@jhimes144 I experienced similar issue, and this fixed it - https://github.com/AvaloniaUI/Avalonia/pull/7948 Doesn’t seemed to be backported into
0.10.x
, did you check with11-preview
s?