473,385 Members | 2,013 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,385 software developers and data experts.

Need 2 forced garbage collections?????

Hi,

I'm writing an MS Outlook 2000 Addin in C#. I have
created C# classes that monitor folder events and do other
business logic. Things work fine until I want to exit
Outlook. When I exit Outlook I have to force 2 garbage
collection cycles in order for all of my folder monitor
classes to get cleaned up {else Outlook will not unload
from memory}. In other words my OnDisconnect() method
looks something like this:

{
// recursively null out all folder objects
myOutlookMonitor = null;

Trace.Writeln("First GC.Collect()...");

GC.Collect();
GC.WaitForPendingFinalizers();

Trace.Writeln("Second GC.Collect()...");

GC.Collect();
GC.WaitForPendingFinalizers();
}

I put Trace calls into the destructors for my folder
monitor classes and indeed I have confirmed that several
of the destructors do not get called until the second
GC.Collect() is issued...

Does anybody know why I would need to issue 2 GC.Collect()
calls? Outlook does succesfully unload from memory after
I issue the second GC.Collect() and it will NOT unload
from memory unless I issue 2 GC.Collect() calls....

--Richard
Nov 15 '05 #1
5 1937

Hi Richard,

I think when you invoke the first time of garbage collection, some object of
you application still did not get release(its reference count >0), so they
will not
get collect.
Then, in the second time, their reference released, so can be succeeded
collect.

Hope this helps,
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Content-Class: urn:content-classes:message
| From: "Richard" <ri******@amgen.com>
| Sender: "Richard" <ri******@amgen.com>
| Subject: Need 2 forced garbage collections?????
| Date: Tue, 9 Sep 2003 16:16:47 -0700
| Lines: 39
| Message-ID: <07****************************@phx.gbl>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcN3KHDYFM6hjJ9CSPS/pDtKXLxUYQ==
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:183617
| NNTP-Posting-Host: TK2MSFTNGXA09 10.40.1.161
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Hi,
|
| I'm writing an MS Outlook 2000 Addin in C#. I have
| created C# classes that monitor folder events and do other
| business logic. Things work fine until I want to exit
| Outlook. When I exit Outlook I have to force 2 garbage
| collection cycles in order for all of my folder monitor
| classes to get cleaned up {else Outlook will not unload
| from memory}. In other words my OnDisconnect() method
| looks something like this:
|
| {
| // recursively null out all folder objects
| myOutlookMonitor = null;
|
| Trace.Writeln("First GC.Collect()...");
|
| GC.Collect();
| GC.WaitForPendingFinalizers();
|
| Trace.Writeln("Second GC.Collect()...");
|
| GC.Collect();
| GC.WaitForPendingFinalizers();
| }
|
| I put Trace calls into the destructors for my folder
| monitor classes and indeed I have confirmed that several
| of the destructors do not get called until the second
| GC.Collect() is issued...
|
| Does anybody know why I would need to issue 2 GC.Collect()
| calls? Outlook does succesfully unload from memory after
| I issue the second GC.Collect() and it will NOT unload
| from memory unless I issue 2 GC.Collect() calls....
|
| --Richard
|
|
|

Nov 15 '05 #2
Any object thtat contains a finalizer (i.e. a destructor) requires at least
two garbage collection cycles. The first cycle detects that it has a
finalizer that must be run and puts the object on a special queue that will
run the finalizer, and the second cycle cleans it up after the finalizer has
run. While the object is on the finalizer queue the object is still
considered reachable.

These articles explain this and many other aspects of GC you should know
about.
http://www.wintellect.com/resources/...00/GCI/GCI.asp

http://www.wintellect.com/resources/.../GCI2/GCI2.asp

"Richard" <ri******@amgen.com> wrote in message
news:07****************************@phx.gbl...
Hi,

I'm writing an MS Outlook 2000 Addin in C#. I have
created C# classes that monitor folder events and do other
business logic. Things work fine until I want to exit
Outlook. When I exit Outlook I have to force 2 garbage
collection cycles in order for all of my folder monitor
classes to get cleaned up {else Outlook will not unload
from memory}. In other words my OnDisconnect() method
looks something like this:

{
// recursively null out all folder objects
myOutlookMonitor = null;

Trace.Writeln("First GC.Collect()...");

GC.Collect();
GC.WaitForPendingFinalizers();

Trace.Writeln("Second GC.Collect()...");

GC.Collect();
GC.WaitForPendingFinalizers();
}

I put Trace calls into the destructors for my folder
monitor classes and indeed I have confirmed that several
of the destructors do not get called until the second
GC.Collect() is issued...

Does anybody know why I would need to issue 2 GC.Collect()
calls? Outlook does succesfully unload from memory after
I issue the second GC.Collect() and it will NOT unload
from memory unless I issue 2 GC.Collect() calls....

--Richard

Nov 15 '05 #3
Richard,
In case you don't already have it, the following link has a number of
resources on using Outlook with .NET.

http://www.microeye.com/resources/res_outlookvsnet.htm

Also, when you are done with an Outlook object (any COM object really) you
should call System.Runtime.InteropServices.Marshal.ReleaseComO bject, rather
than waiting on the GC to do it for you.

Hope this helps
Jay

"Richard" <ri******@amgen.com> wrote in message
news:07****************************@phx.gbl...
Hi,

I'm writing an MS Outlook 2000 Addin in C#. I have
created C# classes that monitor folder events and do other
business logic. Things work fine until I want to exit
Outlook. When I exit Outlook I have to force 2 garbage
collection cycles in order for all of my folder monitor
classes to get cleaned up {else Outlook will not unload
from memory}. In other words my OnDisconnect() method
looks something like this:

{
// recursively null out all folder objects
myOutlookMonitor = null;

Trace.Writeln("First GC.Collect()...");

GC.Collect();
GC.WaitForPendingFinalizers();

Trace.Writeln("Second GC.Collect()...");

GC.Collect();
GC.WaitForPendingFinalizers();
}

I put Trace calls into the destructors for my folder
monitor classes and indeed I have confirmed that several
of the destructors do not get called until the second
GC.Collect() is issued...

Does anybody know why I would need to issue 2 GC.Collect()
calls? Outlook does succesfully unload from memory after
I issue the second GC.Collect() and it will NOT unload
from memory unless I issue 2 GC.Collect() calls....

--Richard

Nov 15 '05 #4
In article <07****************************@phx.gbl>, ri******@amgen.com
says...
Hi,

I'm writing an MS Outlook 2000 Addin in C#. I have
created C# classes that monitor folder events and do other
business logic. Things work fine until I want to exit
Outlook. When I exit Outlook I have to force 2 garbage
collection cycles in order for all of my folder monitor
classes to get cleaned up {else Outlook will not unload
from memory}. In other words my OnDisconnect() method
looks something like this:

{
// recursively null out all folder objects
myOutlookMonitor = null;

Trace.Writeln("First GC.Collect()...");

GC.Collect();
GC.WaitForPendingFinalizers();

Trace.Writeln("Second GC.Collect()...");

GC.Collect();
GC.WaitForPendingFinalizers();
}

I put Trace calls into the destructors for my folder
monitor classes and indeed I have confirmed that several
of the destructors do not get called until the second
GC.Collect() is issued...

Does anybody know why I would need to issue 2 GC.Collect()
calls? Outlook does succesfully unload from memory after
I issue the second GC.Collect() and it will NOT unload
from memory unless I issue 2 GC.Collect() calls....

--Richard


Hi Richard,
I had the same problem. So, the way I solved is:
Make a all references to main Outlook objects (like Outlook.Application,
Outlook.Application.Explorers, etc.) public static. And the event
handlers also.
If you have used a private objects, before leaving the method/part of
code where they are created make:
if (outlookobject != null)
{
while (Marshal.ReleaseObjByReference(outlookobject) > 0);
//this is empty, just to release all wrappers
outlookobject = null;
}

You have to do the above also in methods where you receive an outlook
object as parameter (like in event handlers). You have to make
ReleaseObjByReference on the parameter.
The number of references increase with every call from COM environment
to managed class, so you have to be careful.

Hope that helps
Sunny

P.S. There are a lot of posts in .outlook.program_addins and
..developer.outlook.addins about that. In some of the threads me and
other guys have paste some code. You have to search for 'Outlook stays
in memory' and things like that.
Nov 15 '05 #5

Thanx a million!

Both of the articles were excellent. My code suffers from an
unavoidable case of resurrection...

--Richard

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #6

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

Similar topics

1
by: Bob | last post by:
Are there any known applications out there used to test the performance of the .NET garbage collector over a long period of time? Basically I need an application that creates objects, uses them, and...
6
by: Ganesh | last post by:
Is there a utility by microsoft (or anyone) to force garbage collection in a process without have access to the process code. regards Ganesh
24
by: Yang | last post by:
I found a very strange behavior when I write a C# windows application. If I allocate a huge chunk of memory by an array, the memory will never be released by .NET. The problem can be demostrated...
2
by: Oculus | last post by:
Before I get into the question -- I know .NET isn't the right solution for this app but it's part of my clients requirements and writing this in C++ isn't an option. That being said -- my app is a...
28
by: joe | last post by:
I have a simple .NET application with two or three listViews which are filled with icons and when the user click on the proper item, they display the related images. I use "image = null ; " for all...
3
by: Ian Taite | last post by:
Hello, I'm exploring why one of my C# .NET apps has "high" memory usage, and whether I can reduce the memory usage. I have an app that wakes up and processes text files into a database...
22
by: Tom Wright | last post by:
Hi all I suspect I may be missing something vital here, but Python's garbage collection doesn't seem to work as I expect it to. Here's a small test program which shows the problem on python 2.4...
0
by: adubra | last post by:
Hi there, I am using a device context (DC) and a buffer to successfully draw to screen. However, when I update the DC at very high frame rate and drag the frame containing the image very quickly...
3
by: Kuldeep | last post by:
Hi All, Is there a way to test whether Garbage Collection has been successfully performed for a ASP.NET application. Even if there is a tool to check this, please let me know. Regards,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.