calling gc.collect

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
 
A

Arne Vajhøj

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top