calling gc.collect

  • Thread starter Thread starter mp
  • Start date Start date
M

mp

I thought I'd seen it said one shouldn't call gc.collect directly in most
cases?
i found this snip on the web,
http://csharp.net-informations.com/excel/csharp-open-excel.htm
this is the end of the sample

....Code using xlApp....
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}

private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Unable to release the Object " +
ex.ToString());
}
finally
{
GC.Collect();
}
}
}

I was wondering if in this context the gc.collect is right or is this a bad
example?
thanks
mark
 
I thought I'd seen it said one shouldn't call gc.collect directly in most
cases?
i found this snip on the web,
http://csharp.net-informations.com/excel/csharp-open-excel.htm
this is the end of the sample

...Code using xlApp....
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}

private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Unable to release the Object " +
ex.ToString());
}
finally
{
GC.Collect();
}
}
}

I was wondering if in this context the gc.collect is right or is this a bad
example?

I think it is a bad example.

If the CLR needs the memory, then it will GC.

Arne
 
Back
Top