Re: Lowdown on finalization

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Tom Shelton (tom_at_mtogden.com)
Date: 02/03/04


Date: Mon, 02 Feb 2004 23:19:54 -0800

On 2004-02-03, Frank Rizzo <nospam@nospam.com> wrote:
> Tom Shelton wrote:
>> On 2004-02-03, Frank Rizzo <nospam@nospam.com> wrote:
>>
>>>Can someone give a reader's digest on finalization of objects in .net
>>>framework.
>>>
>>>When do I need to call .Dispose?
>>
>> When your object manages unmanaged resources (ex: Database connections,
>> sockets, file handles, os handles, etc). In these cases you'll want
>> your class to implement the IDisposable interface...
>
> So implementing this interface means what? That when the client calls
> .Dispose method on my object, I'll release whatever needs to be released?
>

Yep.

> How is that different from writing a ReleaseResources method where I
> release all the resources?
>

There is no difference. IDisposable is just a formalized interface to
accomplish this. Basically, it is a convention.

>>
>> And of course, you'll want to call it on any class that implements it :)
>>
>>
>>>What happens when I set the object to Nothing (or null in c#)
>
> So should I set local objects to Nothing or not? What about class level
> variables that I no longer need?
>

Local objects don't need to be set to nothing because once the object is
out of scope and no longer referenced it is eligable for GC. In fact,
the JIT can actually optimize the code so that an local object is available
for GC even before the end of the method... Say you have this scenario:

Private Sub MySub()
        Dim o As New MyObjectType

        o.DoCoolStuff()

        ' Do a lot of stuff that doesn't have anything to do with o
End Sub

Because the method no longer referes to o, the JIT will make it
available for GC as soon as the call to DoCoolStuff is complete... It
seems in the Beta days setting the object to nothing could actually
interfere with this optimization - but I have since read that the JIT
will actually optimize away the assignment to nothing now.

For class level objects, it would probably be a good idea when they are
no longer needed.

> Also, how is Finalize different?
>

Finalize is a method of object. It's purpose is essentially the same as
Dispose - release resources. The difference is that Finalize is called
by the GC, and should be avoided because it can have a negative impact
on the performance of the GC, because it actually takes two passes to
clean up an object that has overridden Finalize.

What you will usually see is that objects that contain unmanaged
resources will usually implement both IDisposable AND override finalize.
The Dispose implementation, will usually call GC.SuppressFinalize to
remove the object from the finalization queue. This way, you can make
sure that your resources will be released eventually - even if the
client forgets to call dispose. But if they do remember, then you don't
incure the performance hit.

One thing I've taken to doing lately, is actually throw an exception
from the finalize method (or you could use an assertion). This sort of
clues me in to when I haven't managed my resources properly :)

>>
>>
>> Not much really... It simply releases the reference, and if there are
>> no other references makes the object eligibale for gc. In the case of
>> local objects, it should usually be avoided...
>>
>>
>>>What else do I need to know?
>>
>>
>> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconimplementingdisposemethod.asp
>
> Thanks, this is good stuff. However, is it me or they made it a lot
> more complicated than it needs to be? You pass true or false into
> Dispose based on the identity of the caller??????? What? Was someone
> smoking at ms? I am starting to miss the lack of backspace in vi.

Just covering the bases. For the most part, resource management is
pretty nice in .NET because the runtime can do most of the work for
you... Unfortunately, ever system has it's drawbacks, and there are
times when you have to take a more active role in the process :)

-- 
Tom Shelton [MVP]
Powered By Gentoo Linux 1.4
The chief cause of problems is solutions.
		-- Eric Sevareid


Relevant Pages

  • Re: Lowdown on finalization
    ... >>How is that different from writing a ReleaseResources method where I ... Dispose method on the oFile object. ... > Dispose - release resources. ... The difference is that Finalize is called ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Lowdown on finalization
    ... > private sub MySub() ... except in this case the Dispose is redundant... ... >> clean up an object that has overridden Finalize. ... >> resources will usually implement both IDisposable AND override finalize. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: GC and Dispose method questions
    ... Its important to realize why IDispose exists, ... finalize method. ... resources will take place and not left up only to the developer. ... Dispose as explained later). ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Destructors in C#????
    ... A destructor in C# is just a syntax shortcut. ... Finalize on your object. ... non managed resources) that should be released as soon as possible. ... Dispose on it before abandoning any reference to the object. ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: what are unmanaged resources
    ... the GC will come along and clean up all the memory and resources associated ... in a deterministic fashion and clean up unmanaged resources. ... placed inside the Dispose functions. ... sooner than later (i.e. by waiting for the garbage collector) and you should ...
    (microsoft.public.dotnet.languages.csharp)