PC Review


Reply
Thread Tools Rate Thread

CLR Garbagge collection

 
 
=?Utf-8?B?Q2FybG9zIExvemFubw==?=
Guest
Posts: n/a
 
      16th Feb 2005
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
 
Reply With Quote
 
 
 
 
Chad Z. Hower aka Kudzu
Guest
Posts: n/a
 
      16th Feb 2005
"=?Utf-8?B?Q2FybG9zIExvemFubw==?="
<(E-Mail Removed)> wrote in
news:694B6BDD-7385-4A26-90E9-(E-Mail Removed):
> 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/
 
Reply With Quote
 
sadhu
Guest
Posts: n/a
 
      16th Feb 2005
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

 
Reply With Quote
 
Chad Z. Hower aka Kudzu
Guest
Posts: n/a
 
      16th Feb 2005
"sadhu" <(E-Mail Removed)> wrote in news:1108533800.835941.41020
@f14g2000cwb.googlegroups.com:
> 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/
 
Reply With Quote
 
Joyjit Mukherjee
Guest
Posts: n/a
 
      16th Feb 2005
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



"Chad Z. Hower aka Kudzu" <(E-Mail Removed)> wrote in message
news:Xns95FFB0AE3738cpub@127.0.0.1...
> "sadhu" <(E-Mail Removed)> wrote in news:1108533800.835941.41020
> @f14g2000cwb.googlegroups.com:
> > 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/



 
Reply With Quote
 
=?Utf-8?B?Q2FybG9zIExvemFubw==?=
Guest
Posts: n/a
 
      17th Feb 2005
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

"Joyjit Mukherjee" wrote:

> 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
>
>
>
> "Chad Z. Hower aka Kudzu" <(E-Mail Removed)> wrote in message
> news:Xns95FFB0AE3738cpub@127.0.0.1...
> > "sadhu" <(E-Mail Removed)> wrote in news:1108533800.835941.41020
> > @f14g2000cwb.googlegroups.com:
> > > 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/

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Øyvind Isaksen Microsoft ASP .NET 1 18th May 2007 10:24 AM
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Øyvind Isaksen Microsoft Dot NET 1 18th May 2007 10:24 AM
Can't get collection to save when using collection of custom class as property of control in VS 2005 J.Edwards Microsoft Dot NET Compact Framework 0 10th Jan 2006 04:44 AM
Showing Garbagge in my File. =?Utf-8?B?QWRuYW4gQWxp?= Microsoft Excel Worksheet Functions 0 12th Sep 2005 10:57 AM
key/value collection that allows key string to be updated and retains collection item entry order dx Microsoft Dot NET Framework 2 25th Sep 2004 05:51 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:52 AM.