How do I avoid a memory leak with LINQ-To-SQL?

后端 未结 5 1897
轮回少年
轮回少年 2020-12-08 15:47

I have been having some issues with LINQ-To-SQL around memory usage. I\'m using it in a Windows Service to do some processing, and I\'m looping through a large amount of da

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-08 16:18

    I just ran into a similar problem. In my case, helped establish the properties of DataContext.ObjectTrackingEnabled to false. But it works only in the case of iterating through the rows as follows:

    using (var db = new DataContext())
    {
        db.ObjectTrackingEnabled = false;
        var documents = from d in db.GetTable()
                         select d;
        foreach (var doc in documents)
        {
            ...
        }
    }
    

    If, for example, in the query to use the methods ToArray() or ToList() - no effect

提交回复
热议问题