CLR Garbagge collection

G

Guest

Hi,

I wrote an application that creates Excel objects in the background. I would
like to dispose these objects once I know they won't be used again.
I call the Excel quit function, and the objects stay there until the creator
application is closed. I also tried calling GC.Collect to force collection
with same results.

Sample code:

private void ExcelObjs()
{
Excel.ApplicationClass oXlApp = new Excel.ApplicationClass();
Excel.Workbook oBook = null;
oBook = oXlApp.Workbooks.Open(sFile,Type.Missing, false, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
true, true, Type.Missing, Type.Missing);

// Additional code here

if (oBook is Excel.Workbook)
oBook.Close(false,Type.Missing,Type.Missing);

// Tried calling GC.Collect to force garbagge collection
int nGeneration = GC.GetGeneration(oXlApp);
oXlApp.Quit();
GC.Collect(nGeneration);
}

I will appreciate any help.

Carlos
 
C

Chad Z. Hower aka Kudzu

"=?Utf-8?B?Q2FybG9zIExvemFubw==?="
private void ExcelObjs()
{
Excel.ApplicationClass oXlApp = new Excel.ApplicationClass();
Excel.Workbook oBook = null;
oBook = oXlApp.Workbooks.Open(sFile,Type.Missing, false,
Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, true, true, Type.Missing, Type.Missing);

// Additional code here

if (oBook is Excel.Workbook)
oBook.Close(false,Type.Missing,Type.Missing);

// Tried calling GC.Collect to force garbagge collection
int nGeneration = GC.GetGeneration(oXlApp);
oXlApp.Quit();
GC.Collect(nGeneration);
}

Collecting the GC wont do anything because the local procedure still has a
reference to it. Try oXlApp.Dispose(), and if it does not have a dispose
then at least try oXlApp = null;

Finally - if it has a finalizer, you need to run the GC twice because
finalzed objects require two passes. You need more than collect, but I dont
remember the exact syntax off the top of my head.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Make your ASP.NET applications run faster
http://www.atozed.com/IntraWeb/
 
S

sadhu

It looks like
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

The second call makes sure that objects which had roots in the
freachable queue are removed.

Calling Dispose() won't cause the GC to collect that object. The GC
should be intelligent enough to work without setting the variable to
null, IMO.

Regards
Senthil
 
C

Chad Z. Hower aka Kudzu

sadhu said:
It looks like
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

Yes thats it!
Calling Dispose() won't cause the GC to collect that object. The GC
should be intelligent enough to work without setting the variable to
null, IMO.

Setting it to null is useful (albeit limited) for objects without Dispose.
But really anything worth disposing should have a Dispose implemented...


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Make your ASP.NET applications run faster
http://www.atozed.com/IntraWeb/
 
J

Joyjit Mukherjee

Hi All,

In my view, instead of fiddling with the GC whose operation is, pretty much
out of our control. Can we try this: -

Marshal.ReleaseComObject(Yourobject)

The Marshal class has excellent capabilities to work with interop objects,
specially COM ones.

Let me know if I am wrong somewhere.

Thanks

Joyjit
 
G

Guest

Hi everybody.
I really appreciate all your feedback.

I tested the code suggested:
a) GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

It did help to dispose the Excel objects, but they are not disposed when
expected. They still stay in memory for 3 t o 5 minutes, then are disposed
randomly.

b) Marshal.ReleaseComObject(oXlApp)
It rose the following exception:
An unhandled exception of type
'System.Runtime.InteropServices.InvalidComObjectException' occurred in
CheckReturns.exe
Additional information: COM object that has been separated from its
underlying RCW can not be used.

Do you have any other ideas?

Carlos Lozano
www.caxonline.net
 

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