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

后端 未结 5 1896
轮回少年
轮回少年 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:34

    A DataContext tracks all the objects it ever fetched. It won't release this until it is garbage collected. Also, as it implements IDisposable, you must call Dispose or use the using statement.

    This is the right way to go:

    using(DataContext myDC = new DataContext)
    {
      //  Do stuff
    } //DataContext is disposed
    

提交回复
热议问题