Memory leak: ObjectEqualityComparer retained after LINQ Operations

2 weeks ago 8
ARTICLE AD BOX

When running LINQ queries such as Where, Except, Intersect, Union, and FirstOrDefault on a List, the memory profiler shows an instance of ObjectEqualityComparer being retained and observed as a memory leak in the profiler.

Without these LINQ operations, the ObjectEqualityComparer is not retained.

It is unclear whether this is expected behavior or not. I am filing this issue to request clarification on the following:

A) Is this expected behavior?
B) If it is expected behavior, should this be treated as "not an issue"?
C) If it is not expected behavior, please provide guidance on how to resolve or avoid this memory leak?

public partial class MainWindow : Window { private List<CustomColumn> _listOfColumns; public List<CustomColumn> ListOfColumns { get { return _listOfColumns; } set { _listOfColumns = value; } } public MainWindow() { InitializeComponent(); } private void CallingLINQQueriesButton_Click(object sender, RoutedEventArgs e) { _listOfColumns = new List<CustomColumn>(); for (int i = 0; i < 5; i++) { _listOfColumns.Add(new CustomColumn()); } // Codes causing memory leak _listOfColumns.Except(_listOfColumns.Where(x => x == null)).ToList(); _listOfColumns.Intersect(_listOfColumns.Where(x => x != null)).ToList(); _listOfColumns.Where(x => x != null).ToList(); _listOfColumns.FirstOrDefault(x => x != null); } }

Profiler Result: Running the LINQ Queries

profiler image after running the LINQ queries and before calling dispose

Profiler Result: After Disposing the List

profiler image after running the LINQ queries and calling dispose

Path to roots

Path To Roots

Read Entire Article