By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,761 Members | 1,009 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,761 IT Pros & Developers. It's quick & easy.

Garbage Collection - dim x as y versus dim x as new y

P: n/a
with respect to garbage collection. Does it make any difference? My current
understanding is that (to avoid memory leaks, ie memory accumulation - never
letting go of the memory something has referenced) it isn't so much HOW an
object reference (or string or byte())
is created, it is more about no longer having a reference to an object or
other type.
If so, how do I remove a reference to something when I no longer need it?
thx, -hazz

dim x as new y
try
x.foo()

dim x as y
try
x = new y
x.foo()


Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies

P: n/a
Well if the object goes out of scope it will automaticly be released
although i am still one of those people who like to set object pointers to
Nothing
however in the example you gave setting it to a new object pointer is
sufficient

dim x as new y
try
x.foo()

' dim x as y ------- this is not necessary as the declaraion is above the
try statement it is still in scope
try
x = new y
x.foo()
regards

Michel Posseth


"hazz" <hazz@sonic_net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
with respect to garbage collection. Does it make any difference? My
current understanding is that (to avoid memory leaks, ie memory
accumulation - never letting go of the memory something has referenced) it
isn't so much HOW an object reference (or string or byte())
is created, it is more about no longer having a reference to an object or
other type.
If so, how do I remove a reference to something when I no longer need it?
thx, -hazz

dim x as new y
try
x.foo()

dim x as y
try
x = new y
x.foo()

Nov 21 '05 #2

P: n/a
thank you michel. If an object or type goes out of scope, it is elgible for
garbage collection. I like that simple concept.
-greg

"m.posseth" <mi*****@nohausystems.nl> wrote in message
news:e$**************@TK2MSFTNGP10.phx.gbl...
Well if the object goes out of scope it will automaticly be released
although i am still one of those people who like to set object pointers to
Nothing
however in the example you gave setting it to a new object pointer is
sufficient

dim x as new y
try
x.foo()

' dim x as y ------- this is not necessary as the declaraion is above the
try statement it is still in scope
try
x = new y
x.foo()
regards

Michel Posseth


"hazz" <hazz@sonic_net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
with respect to garbage collection. Does it make any difference? My
current understanding is that (to avoid memory leaks, ie memory
accumulation - never letting go of the memory something has referenced)
it isn't so much HOW an object reference (or string or byte())
is created, it is more about no longer having a reference to an object or
other type.
If so, how do I remove a reference to something when I no longer need it?
thx, -hazz

dim x as new y
try
x.foo()

dim x as y
try
x = new y
x.foo()


Nov 21 '05 #3

P: n/a

"hazz" <hazz@sonic_net> wrote in message
news:en**************@TK2MSFTNGP14.phx.gbl...
:
: thank you michel. If an object or type goes out of scope, it is
: elgible for garbage collection. I like that simple concept.
Ralf:
With one caveat: if there are two or more references to the same
instance of an object, it will not be collected until all references are
released.
Ralf
--
----------------------------------------------------------
* ^~^ ^~^ *
* _ {~ ~} {~ ~} _ *
* /_``>*< >*<''_\ *
* (\--_)++) (++(_--/) *
----------------------------------------------------------
There are no advanced students in Aikido - there are only
competent beginners. There are no advanced techniques -
only the correct application of basic principles.
Nov 21 '05 #4

P: n/a

Actually, the .NET Garbage Collector is a mark & sweep collector. When a GC
pass starts, the first part of the process is to run through memory from
known roots - the stack (for each thread) and global data are two such
roots. There is another major root in the .NET GC which is used for
performance optimization, but the concept is the same. In this pass, every
object that is reached is marked and a table of used memory is built.
During the second pass, objects are moved to fill in holes left by the
initial pass and all the references to those objects are updated to point to
the new locations. The end result of this pass is that all available memory
is at the end of the heap in one big contiguous block, which the memory
allocater can either reuse or return to the OS.

A reference counted GC, such as the one in VB6 can leak memory when objects
refer to themselves, either directly or indirectly.

Mike.
"_AnonCoward" <ab*@xyz.com> wrote in message
news:7i*********************@twister.southeast.rr. com...

"hazz" <hazz@sonic_net> wrote in message
news:en**************@TK2MSFTNGP14.phx.gbl...
:
: thank you michel. If an object or type goes out of scope, it is
: elgible for garbage collection. I like that simple concept.
Ralf:
With one caveat: if there are two or more references to the same
instance of an object, it will not be collected until all references are
released.
Ralf
--
----------------------------------------------------------
* ^~^ ^~^ *
* _ {~ ~} {~ ~} _ *
* /_``>*< >*<''_\ *
* (\--_)++) (++(_--/) *
----------------------------------------------------------
There are no advanced students in Aikido - there are only
competent beginners. There are no advanced techniques -
only the correct application of basic principles.


Nov 21 '05 #5

P: n/a
Hazz,

An object will be released by the GC if it has no reference anymore to
another object, and when no other object is anymore referencing it.

Think for that on the DataTable in a Dataset. It has a reference to the
dataset that holds it and it is in the collection of the dataset. Both
references has to be nothing to let the GC release the datatable.

An other example is just this

Private mysub()
dim dt as new datatable
datagrid1.datasource = dt
end sub

Although it goes itself out of scope, will the datatable not be released by
the GC until datagrid1.datasource = nothing

And therefore you can get the reference than forever back as long as it is
not nothign by this
dt as datatable = directcast(datagrid1.datasource,datatable).datasou rce

I hope this helps,

Cor
Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.