The runtime (CLR) never calls the dispose method. It is upto the client
using the object to call dispose. There are a couple of exceptions. Read
this for more information:
http://blogs.msdn.com/clyon/archive/2004/09/21/232445.aspx
So whether you just add a Dispose method or implement IDisposable interface,
its upto the application using your class to call the dispose method.
Ofcourse, it makes more sense to implement IDisposable since that is indeed
why the interface is there - for one to write dispose code. If you simply
write a Sub Dispose, it could be misleading to someone using your class
since one would expect an implementation of IDisposable if one were to
follow the standards.
hope that helps..
Imran.
Ok Imran, then let me ask you this...
When the GC does eventually collect the garbage, what determines which
objects are collected and which are not? Is is simply that any object that
is no longer referenced are the ones collected?
> The runtime (CLR) never calls the dispose method. It is upto the client
> using the object to call dispose. There are a couple of exceptions. Read
[quoted text clipped - 19 lines]
>>
>> Thanks.
Larry Serflaten - 21 Oct 2004 09:59 GMT
"Scott M." <NoSpam@NoSpam.com> wrote
> Ok Imran, then let me ask you this...
>
> When the GC does eventually collect the garbage, what determines which
> objects are collected and which are not? Is is simply that any object that
> is no longer referenced are the ones collected?
Its not quite that simple. The GC is good at cleaning up short lived objects,
but those that hang around for a while get shoved on 'the back burner' (meaning
they don't get tested as often).
The GC is also pretty good at managing those long lived objects, but its the
middle ground that needs to be addressed. That area is dependant on your
own use of memory, so just how often the GC runs, and decides to move
things to the 'back burner' is partially dependant on what you're doing....
FWIW:
The proper term is 'generations'. Generation 0 items come and go as needed....
So, its those Gen 0 items that are swept away when nothing holds a reference
to them. If they survive that first look, then get shoved into the Gen 1 area, and
then Gen 2, etc where they don't go looking for memory as often as the Gen 0 area.
For a bit more on the GC, you might want to watch the .Net show where one
of the CLR's architect was interviewed:
http://msdn.microsoft.com/theshow/Episode027/default.asp
LFS
Imran Koradia - 21 Oct 2004 13:00 GMT
Scott,
As Larry mentioned, collection is a bit more complicated. I think you are
confusing a finalize method with the dispose method (I could be wrong
though). If an object has a finalizer, it is not collected (when a
collection is performed by the GC and it finds that the object is not
referenced anymore) but instead is pushed onto a finalization queue so that
the runtime can execute the finalize method of the object. And then there is
the concept of various generations (0, 1 and 2 in the current framework
version). I would suggest you read these 2 excellent articles by Jeffrey
Richter on Garbage collection which also talks about finalization and
resurrection:
http://msdn.microsoft.com/msdnmag/issues/1100/GCI/
http://msdn.microsoft.com/msdnmag/issues/1200/GCI2/default.aspx
Also, here is a blog post on dispose dos and don'ts by Chris Lyon which you
might be interested in since you are deciding to implement dispose in your
class:
http://blogs.msdn.com/clyon/archive/2004/09/23/233464.aspx
hope that helps..
Imran.
> Ok Imran, then let me ask you this...
>
[quoted text clipped - 25 lines]
>>>
>>> Thanks.