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

H

hazz

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()
 
M

m.posseth

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
 
H

hazz

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

_AnonCoward

:
: 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
--
 
M

Michael D. Ober

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.
 
C

Cor Ligthert [MVP]

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).datasource

I hope this helps,

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top