473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

CLR Garbagge collection

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
Nov 16 '05 #1
5 2713
"=?Utf-8?B?Q2FybG9zIExvemFubw==?="
<Ca**********@discussions.microsoft.com> wrote in
news:69**********************************@microsof t.com:
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/
Nov 16 '05 #2
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

Nov 16 '05 #3
"sadhu" <se**********@wdevs.com> 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/
Nov 16 '05 #4
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" <cp**@hower.org> wrote in message
news:Xn*****************@127.0.0.1...
"sadhu" <se**********@wdevs.com> 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/

Nov 16 '05 #5
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.InvalidComObjectEx ception' 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" <cp**@hower.org> wrote in message
news:Xn*****************@127.0.0.1...
"sadhu" <se**********@wdevs.com> 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/


Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Generic Usenet Account | last post by:
To settle the dispute regarding what happens when an "erase" method is invoked on an STL container (i.e. whether the element is merely removed from the container or whether it also gets deleted in...
5
by: Kurt Bauer | last post by:
I have an ASP group calendar application which pulls calendar data from Exchange via webdav into an XML string. I then loop the XML nodes to populate a collection of appointments. Finally I use...
18
by: Scott | last post by:
I have a collection where the items in the collection are dates. I want to iterate over the collection and build a value list string for the rowsource of a listbox. The dates in the collection are...
11
by: Pavils Jurjans | last post by:
Hello, There's some confusion about the purpose and difference between these handy classes... First, both of them are holding number of key - value pairs, right? Then, I see that there may be...
2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
6
by: Michael D. Ober | last post by:
In VB 6, the loop iterator v in the following code must be a variant. dim v as variant dim c as new collection for each v in collection ... next v What is the general translation in VB 7.1...
3
by: Matt Michael | last post by:
I have a listview control and a collection object right now that I'm trying to pass information to and from. Whenever I click on the checkbox, I want it to remove certain listview items and add...
10
by: Chet Cromer | last post by:
I am creating a set of base classes and sub classes to use throughout a program I'm developing. The base class represents a generic "lookup table" from my database that contains lists of things...
6
by: Scott M. Lyon | last post by:
As I mentioned in my other post, I'm attempting to, using COM Interop so I can update existing VB6 code to (for several specific functions) return a Hashtable from a .NET library. I've had...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.