LINQ datacontext and Dispose

Source: Internet
Author: User
Tags server memory
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.