Home All Groups Group Topic Archive Search About

Object deleted or memory leak?

Author
24 Jul 2006 11:08 PM
tshad
If I create an object (instantiate) then do it again using the same Pointer,
will I leave the old object hanging around wasting memory?

Would this create a memory leak or would it be taken care of when I leave
the function?

**********************************************************
Sub ActiveJobEdit_Click(s as Object, e as ImageClickEventArgs)
  Dim newPosition as Position = new Position(oLabel.Text)
....
  newPosition = new Position(otherLabel.Text)
  session("newPosition) = newPosition
End Sub
**********************************************************

Thanks,

Tom

Author
25 Jul 2006 12:08 AM
iwdu15
the great thing about VB is that (almost?) all of your memory management is
takin care of for you. the GC comes and takes care of any unused objects
every so often, once u exit the sub, all local variables are GCed
--
-iwdu15
Author
25 Jul 2006 12:10 AM
Tom Shelton
tshad wrote:
Show quoteHide quote
> If I create an object (instantiate) then do it again using the same Pointer,
> will I leave the old object hanging around wasting memory?
>
> Would this create a memory leak or would it be taken care of when I leave
> the function?
>
> **********************************************************
>  Sub ActiveJobEdit_Click(s as Object, e as ImageClickEventArgs)
>   Dim newPosition as Position = new Position(oLabel.Text)
> ...
>   newPosition = new Position(otherLabel.Text)
>   session("newPosition) = newPosition
>  End Sub
> **********************************************************
>
> Thanks,
>
> Tom

What you are doing would not create a memory leak - but, it also
wouldn't be taken care of when you leave the function...  At least,
probably not :)

See, object life time in .NET is non-deterministic.  It is controled by
the garbage collector, and that generally only runs when your
application is under memory pressure.      Here is an article that
gives a rundown of Memory management in .NET

http://msdn.microsoft.com/msdnmag/issues/1100/GCI/default.aspx
http://msdn.microsoft.com/msdnmag/issues/1200/GCI2/default.aspx

The code is C# and the articles are a bit old, but it covers most of
the basics.  You can also read the documentation, for more details.

--
Tom Shelton [MVP]
Author
25 Jul 2006 5:32 AM
Cor Ligthert [MVP]
Tshad,

Because there are two answers sent in the same time, I would keep it by the
answer from Iwdu15, and if you have a program that is working in the area of
science or something like that, than read the message from Tom.

Just my thought,

Cor

Show quoteHide quote
"tshad" <tscheider***@ftsolutions.com> schreef in bericht
news:%23pBkfY3rGHA.2292@TK2MSFTNGP05.phx.gbl...
> If I create an object (instantiate) then do it again using the same
> Pointer, will I leave the old object hanging around wasting memory?
>
> Would this create a memory leak or would it be taken care of when I leave
> the function?
>
> **********************************************************
> Sub ActiveJobEdit_Click(s as Object, e as ImageClickEventArgs)
>  Dim newPosition as Position = new Position(oLabel.Text)
> ...
>  newPosition = new Position(otherLabel.Text)
>  session("newPosition) = newPosition
> End Sub
> **********************************************************
>
> Thanks,
>
> Tom
>