HighTechTalks DotNet Forums  

Releasing Objects in C'# so that GC can reclaim the memory.

Dotnet Framework (Performance) microsoft.public.dotnet.framework.performance


Discuss Releasing Objects in C'# so that GC can reclaim the memory. in the Dotnet Framework (Performance) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Chris Arnold
 
Posts: n/a

Default Releasing Objects in C'# so that GC can reclaim the memory. - 09-20-2007 , 05:54 AM






Hi All,

I have a number of questions related to making objects available for
collection by the GC. I have an application that is leaking memory to the
point of filling up all 3 heap generations and then throwing
OutOfMemoryException.

Question 1

I class has a property "public IList<SomeClass> MyCollection". In this
class' Dispose(bool) method I am setting this.MyCollection null. Do I need
to remove all the objects from the list before doing this? My worry is that
the items in the list will become 'orphaned' yet still referenced and,
therefore, passed over by the GC.

Question 2

An object will only be 'removed' by the GC if it is no longer referenced by
anything. If I have wired up one of that object's events to an event handler
do I need to remove it first (e.g. myObject.MyEvent -= new
EventHandler(this.eventhandler) ?

Question 3

I create a new object and wire up its Success event to an event handler
method. I then start this object running on a new thread (using
Thread.Start()). When this event is handled and the object has finished
executing, will it automatically go out of scope? Or, in the event handler
to I have to first unwire the event (again using the -= syntax from Q2)?

Many thanks, in advance.

Chris



Reply With Quote
  #2  
Old   
Ben Schwehn
 
Posts: n/a

Default Re: Releasing Objects in C'# so that GC can reclaim the memory. - 09-20-2007 , 08:54 AM






Quote:
I class has a property "public IList<SomeClass> MyCollection". In this
class' Dispose(bool) method I am setting this.MyCollection null. Do I
need to remove all the objects from the list before doing this?
no

Quote:
An object will only be 'removed' by the GC if it is no longer referenced
by anything. If I have wired up one of that object's events to an event
handler do I need to remove it first (e.g. myObject.MyEvent -= new
EventHandler(this.eventhandler) ?
no, an eventhandler does not keep the object alive


Quote:
I create a new object and wire up its Success event to an event handler
method. I then start this object running on a new thread (using
Thread.Start()). When this event is handled and the object has finished
executing, will it automatically go out of scope? Or, in the event
handler to I have to first unwire the event (again using the -= syntax
from Q2)?
no same as 2, if the object is not referenced anymore, the handler will
not keep it alive.


You can use WinDbg to find out what objects are filling your heap.

There used to be free profiler that showed you the usage of the heap
without such a steep lerning curve (compared to using windbg), but I
can't seem to find it atm, your search engine of choice might help you.

Ben


Reply With Quote
  #3  
Old   
Henning Krause [MVP - Exchange]
 
Posts: n/a

Default Re: Releasing Objects in C'# so that GC can reclaim the memory. - 09-20-2007 , 11:39 AM



Hello,

"Ben Schwehn" <ben (AT) bschwehn (DOT) de> wrote

Quote:
I class has a property "public IList<SomeClass> MyCollection". In this
class' Dispose(bool) method I am setting this.MyCollection null. Do I
need to remove all the objects from the list before doing this?

no

An object will only be 'removed' by the GC if it is no longer referenced
by anything. If I have wired up one of that object's events to an event
handler do I need to remove it first (e.g. myObject.MyEvent -= new
EventHandler(this.eventhandler) ?

no, an eventhandler does not keep the object alive
this is contrary to my experience (and contrary to
http://diditwith.net/2007/03/23/Solv...Handlers.aspx).

If I have an object A which points to object B via an eventhandler, object B
is not discarded until object A is also collected.

Quote:
You can use WinDbg to find out what objects are filling your heap.

There used to be free profiler that showed you the usage of the heap
without such a steep lerning curve (compared to using windbg), but I
can't seem to find it atm, your search engine of choice might help you.
A very nice profiler is the one from SciTech (http://memprofiler.com/). They
do have a thirty day trial version.

Kind regards,
Henning Krause



Reply With Quote
  #4  
Old   
Ben Schwehn
 
Posts: n/a

Default Re: Releasing Objects in C'# so that GC can reclaim the memory. - 09-20-2007 , 12:09 PM



Quote:
An object will only be 'removed' by the GC if it is no longer
referenced by anything. If I have wired up one of that object's events
to an event handler do I need to remove it first (e.g.
myObject.MyEvent -= new EventHandler(this.eventhandler) ?

no, an eventhandler does not keep the object alive

this is contrary to my experience (and contrary to
http://diditwith.net/2007/03/23/
SolvingTheProblemWithEventsWeakEventHandlers.aspx) .

If I have an object A which points to object B via an eventhandler,
object B is not discarded until object A is also collected.
I think the scenario the OP was refering to is:

GreatObject foo = new GreatObject();
foo.SomeEvent += new EventHander(bla);

I am pretty certain that in this scenario just because there is an
eventhandler connection, the object will still be eligable for garbage
collection once it goes out of scope because the object is unreachable
(has no GC-Root).


Am I wrong?

Ben



Reply With Quote
  #5  
Old   
Ben Schwehn
 
Posts: n/a

Default Re: Releasing Objects in C'# so that GC can reclaim the memory. - 09-20-2007 , 12:16 PM



Quote:
If I have an object A which points to object B via an eventhandler,
object B is not discarded until object A is also collected.
Ok, i get it now, you're talking about a different scenario than I am.
Not sure what the OP meant. It would be pretty bad if the garbage
collector did collect object B in your scenario as you would then have to
excplicitly reference object B to prevent it from being collected just to
make your eventhandling logic work.

Ben


Reply With Quote
  #6  
Old   
Henning Krause [MVP - Exchange]
 
Posts: n/a

Default Re: Releasing Objects in C'# so that GC can reclaim the memory. - 09-20-2007 , 12:42 PM



Hello,

Quote:
GreatObject foo = new GreatObject();
foo.SomeEvent += new EventHander(bla);
In this case, foo has a strong reference to the object of the bla method.

So in this case, instance which "hosts" the bla method is kept alive until
foo goes out of scope.

Kind regards,
Henning



Reply With Quote
  #7  
Old   
Ben Schwehn
 
Posts: n/a

Default Re: Releasing Objects in C'# so that GC can reclaim the memory. - 09-20-2007 , 12:47 PM



On Thu, 20 Sep 2007 18:42:03 +0200, Henning Krause [MVP - Exchange] wrote:
Quote:
GreatObject foo = new GreatObject();
foo.SomeEvent += new EventHander(bla);

In this case, foo has a strong reference to the object of the bla
method.

So in this case, instance which "hosts" the bla method is kept alive
until foo goes out of scope.

Yes, and any other behaviour would be *very* counter inintuitive!

Ben


Reply With Quote
  #8  
Old   
Henning Krause [MVP - Exchange]
 
Posts: n/a

Default Re: Releasing Objects in C'# so that GC can reclaim the memory. - 09-20-2007 , 05:06 PM



Hi Ben,

Correct. But that was IMHO the question of the original poster.

Kind regards,
Henning


"Ben Schwehn" <ben (AT) bschwehn (DOT) de> wrote

Quote:
On Thu, 20 Sep 2007 18:42:03 +0200, Henning Krause [MVP - Exchange] wrote:
GreatObject foo = new GreatObject();
foo.SomeEvent += new EventHander(bla);

In this case, foo has a strong reference to the object of the bla
method.

So in this case, instance which "hosts" the bla method is kept alive
until foo goes out of scope.


Yes, and any other behaviour would be *very* counter inintuitive!

Ben


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.