LINQ datacontext and Dispose
A question extends developers have with LINQ and databases is this:
How important is it to call dispose on a datacontext object?
Normally a quick Web search gives an easy answer to common questions like this. But sometimes Google fails us. It turns out so many people have attempted to write about thisWrong or incomplete informationThat a web search returns mostly junk.
Here is a definitive answer, which I have finally learned after reading down to comment one hundred and twenty seven on a blog post by scottgu, who runs the ASP. NET team at Microsoft.
Short answer: it is generally not critical to call dispose on datacontext.
Longer answer:
All objects in. net are eventually disposed automatically by the automatic garbage collector. the reason developers are paranoid about calling it explicitly is that if an object contains an expensive resource like an open database connection, then we can't afford to wait around for garbage collection, it wowould have a big impact on scalability.
The good news is that datacontext objectsDo notKeep open database connections like some ADO. Net objects do, so it doesn't really hurt to let the Garbage Collector do it for you.
There is one caveat here though: The datacontext does track change states for the data, and by disposing it You wowould release this memory more quickly. in almost all apps this difference wont be significant. however if you have a Web site that is using all memory available this is one of your optimizations you cocould make.
BTW, a gigabyte of expensive ECC server memory is going for as little as 30 buckets now, so if your server is strained you can help it out pretty cheaply.
Reprinted from: http://lee.hdgreetings.com/2008/06/linq-datacontex.html