I am develope multithread application whish use connections to PostgreSQL database.
I have some trivial queries like this:
Code: Select all
internal BaseFlightList ExecSearching()
{
BaseFlightList flightList = new BaseFlightList(mask.Date);
DBDataContext context = new DBDataContext();
try
{
try
{
DateTime date = DateTime.Now;
var f = from flight in context.fl
where (( flight.dat == date )&& (mask.DBAirline != string.Empty ? flight.carrier == mask.DBAirline : true) && ((mask.DBNumber != string.Empty && mask.DBNumber != XMLBase.mUnknown) ? flight.number == mask.DBNumber : true))
select flight;
if (f.Count() > 0)
{
foreach (fl FL in f)
{
flightList.AddRecord(FLToBFR(FL));
}
}
}
catch (Exception ex)
{
//...
}
}
finally
{
if (context != null)
{
context.Connection.Close();
context.Dispose();
}
}
}
Than I write some test application where I just create DataConext and Dispose it in cycle.
Code: Select all
for (int i = 0; i < 100000; i++)
{
UC.John.DBDataContext.SITABroker.DataContext context = null;
try
{
context = new UC.John.DBDataContext.SITABroker.DataContext();
}
finally
{
if (context != null)
{
context.Connection.Close();
context.Dispose();
}
}
}
So I know that
But it seems that resources that DataContext used is never releases in my applcation even I useHowever, it is not possible to predict when garbage collection will occur...[MSDN]
Code: Select all
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
So my question is:
1. If there are some features in using DataContext in multithread applications
2. How solve the problem with memory using increase? I know that I do something wrong, So can you explain me, what?
Thanx