Re: Garbage Collection

From: Bill McCarthy (no)
Date: 03/30/04


Date: Tue, 30 Mar 2004 19:43:52 +1000

Hi Michael,

If you can explicitly call Dispose, it is always best to do this as this
provides timely release of resources the object may be holding onto. If you
don't call Dispose, the GC will eventually collect them for you once they go out
of scope, but that may take some time, and system resources could get low if any
of the objects waiting to be collected are holding onto resources, such as
bitmaps, brushes, fonts, or just large objects that take up large amounts of
memory.
So, the general rule is if you can safely call IDisposable.Dispose on the
object, then do so.

HTH's

Bill.

"Michael Hart" <me-donotspam@bigpond.net.au> wrote in message
news:%23Sv84ojFEHA.3456@tk2msftngp13.phx.gbl...
> I have a form that creates new objects in all sorts of fashion using vb.net.
>
> Some are created when the form opens with
>
> Private varName as new Object()
>
> just near the form class
>
> In some subs I have
>
> Sub Internal1()
> Dim myVar as new Object()
> End Sub
>
> in the declaration section
>
> and also sometimes I have the constructer separate to the declaration
>
> Sub Internal2()
> Dim newVar as Object
>
> newVar = new Object(parm1, parm2)
> End Sub
>
> Do I need to explicitly dispose of these objects or will the garbage
> collector [if there is one] take care of it when they go out of scope?
>
> When I do a Me.Close on a form does it invoke the garbage collector on the
> form?
>
>
>
>
>
>



Relevant Pages

  • 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: Memory leak with Datagrid and array
    ... So, I close, dispose and set the datagrid=nothing. ... The .net help says "When a form is closed, all resources created within the ... > Matrix_A.DisposeThe data is still in the datagrid so do not forget to set ... >> Private Sub OpenA() ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Disposing of Forms.
    ... again, and therefore keep it around), you should call Dispose on it. ... will release any resources that it is holding that you don't want to wait ... case), or other unmanaged resources. ... > If I display a form using the ShowDialog method, when the form is closed> should I have to explicitly call dispose on the form in order to ensure it> releases its resources, or should this be handled by the garbage collector> when the method that instantiates and opens the form goes out of scope? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Confused about GC & Disposing: How to get it right?
    ... > class that uses instances of objects that have the dispose method, ... Should I conclude that every class that owns resources that have ... > dispose method should also use the Dispose Design Pattern? ... > Protected Overloads Overridable Sub Dispose ...
    (microsoft.public.dotnet.framework)
  • 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)