Memory issue in an WPF application when using linq

131 Views Asked by At

I am currently hunting down memoryleaks in our application and when it comes to viewmodels that execute any linq queries I find an object in memory with that namespace. I am using dotMemory to do the inspection and it lists the object with a +<>c ending. I have not found any explanation what kind of object this is and if it is a real issue that this resides in memory, but I have found out that it is connected to the linq query. Code that reprodce this and as you can see the Linq query result is never used.

 public class myViewModel : PropertyChangedBase
 {

    public myViewModel()
    {
        var memissue = _dummyList.ToList().Any(c => c == false);
    }

    public string SomeBoundProperty
    {
        get { return _someBoundProperty; }
        set
        {
            if (value == _someBoundProperty) return;
            _someBoundProperty = value;
            NotifyOfPropertyChange();
        }
    }
 }

Snapshot from dotmemory: enter image description here

I hope that someone can explain what kind of object a +<>c is and maybe why it is not released from memory or is this just the way that Linq works?

1

There are 1 best solutions below

7
On

.ToList() enumerates the whole collection and stores a copy in memory. This is typically not useful if you aren't going to use all the information in the collection. Try removing .ToList() and see what happens. You should expect to see immediate savings.

To shed some light on the +<>c thing, I believe that comes from it being a generic type. I'm inferring from the code you provided that the type is really List<Boolean>.