In a C# 2008 application, I am using linq to sql statements that look like the following:
Case 1:
using (RPTDataContext rptData = new RPTDataContext())
{
var RPTupdate = (from a in rptData.Trackings
where a.Package_ID == pkgId
select a).SingleOrDefault();
}
Case 2:
TDataContext TData = new TDataContext();
var TNumber = (from dw in cms_TData.people
where dw.Organization_Name.ToUpper().Trim() == strOrgnizationName.Trim()
I am asking that question since an application I am working on is having a memory leak. I am trying to
rule out the above statements as the reason for the memory leak.
My question is could either of this statements cause a memory leak, leave a database connection open,
keep the database object 'alive' in memory? If so, can you tell me what statement could cause that kind of
problem and how to write the code so the problem does not persist?
Loading
VulpesPosted Feb 7, 2013, 5:18 PM
In the second case, there's no 'using' statement and Dispose() is not being called on TData when you've finished with it. This means that the release of resources will need to wait until there are no longer any references to TData when they will be cleaned up just before the garbage collector destroys the object and reclaims the memory it was using. However, the LINQ query contains a reference to TData and so this may not happen for quite some time.
If I were you, I'd therefore use a 'using' statement for the second case as well.