garbage collection method

G

Guest

Could someone please verify if what I am doing as follow is corrected:

1. when dealing with custom class objects:
.....
public myObject as myClass
myObject as New myClass
.......here I am going to fill up myObject with info....tons of them
myObject = nothing
System.GC.Collect()

Is this correct? I don't want to directly include System.GC.Collect() into
myClass terminate sub because there are soooo many local variables to mark
for delete. So will this works or there is another better way?



2. When working with form class, where my form class is named "myForm"
myForm.DefInstance = nothing
System.GC.Collect()

or should I replace those two lines with:
myForm.DefInstance.Dispose(True)

Which one garantee that data will be deleted from machine memory?

Thanks.
 
D

David Browne

Ben said:
Could someone please verify if what I am doing as follow is corrected:

1. when dealing with custom class objects:
....
public myObject as myClass
myObject as New myClass
......here I am going to fill up myObject with info....tons of them
myObject = nothing
System.GC.Collect()

Is this correct?

No. You should not call GC.Collect(). The runtime will run the garbage
collector when it needs to, and very rarely requires manuall intervention.
The whole point of having a Garbage Collectior is to free you, the
developer, from having to manage the memory for your objects. You create
the objects when you need them, and the runtime cleans them up when they are
no longer needed.
2. When working with form class, where my form class is named "myForm"
myForm.DefInstance = nothing
System.GC.Collect()

or should I replace those two lines with:
myForm.DefInstance.Dispose(True)

If an object implements the IDisposable interface then you should invoke its
..Dispose() method when you are done with it. Still no GC.

David
 
G

Guest

David Browne said:
No. You should not call GC.Collect(). The runtime will run the garbage
collector when it needs to, and very rarely requires manuall intervention.
The whole point of having a Garbage Collectior is to free you, the
developer, from having to manage the memory for your objects. You create
the objects when you need them, and the runtime cleans them up when they are
no longer needed.



So in this case, calling:
myObject = nothing
is enough to discard my object from memory? Yes, you're right. But I was
confused b/c the migration wizard warned me that my object will not be
discarded from memory unless garbage collected? So, I thought I had to
directly invoke the garbage collector. Yeah, you're right that don't make
sense to manually manage memory. So setting myObject = nothing is enough
right?


If an object implements the IDisposable interface then you should invoke its
..Dispose() method when you are done with it. Still no GC.



myForm is an instance of my entire class "form" that contains many
components. Which one would makes more senses:
myForm.DefInstance.Dispose(True)
or
myForm.DefInstance = nothing
b/c the second one got me a warning message from migration wizard that my
object will not be discarded ....unless garbage collected.


thanks.
 
G

Guest

Here, I would like to add an example from my class codes:

.....

'UPGRADE_NOTE: Class_Terminate was upgraded to
'Class_Terminate_Renamed.
Private Sub Class_Terminate_Renamed()
'Destroys collection when this class is terminated
'UPGRADE_NOTE: Object mCol may not be destroyed until it is
'garbage collected.
mCol = Nothing

End Sub

Protected Overrides Sub Finalize()
Class_Terminate_Renamed()
MyBase.Finalize()
......


so in this case, I'd thought that garbage collector automatically handles
mCol. Then how come migration wizard still complaining that I have not
called to garbage collector?
 
T

Tom Shelton

So in this case, calling:
myObject = nothing
is enough to discard my object from memory? Yes, you're right. But I was
confused b/c the migration wizard warned me that my object will not be
discarded from memory unless garbage collected? So, I thought I had to
directly invoke the garbage collector. Yeah, you're right that don't make
sense to manually manage memory. So setting myObject = nothing is enough
right?

Object lifetimes are different in .NET then they are in VB6. An object
will continue to exist, even after set to nothing until the next GC cycle.
But in the general case, starting that cycle manually defeats the whole
purpose of the GC.

In fact, if it's a local object, I wouldn't bother even setting the
object to nothing - since that will happen anyway when the procedure
ends. I would only do that with stuff that has class/module scope.
myForm is an instance of my entire class "form" that contains many
components. Which one would makes more senses:
myForm.DefInstance.Dispose(True)
or
myForm.DefInstance = nothing
b/c the second one got me a warning message from migration wizard that my
object will not be discarded ....unless garbage collected.

That warning is just letting you know that object lifetimes are
different in .NET. VB.NET has what is being called "non-deterministic
finalization". What that means is that an object is not immediately
destroyed when the last reference is removed. In fact, it may continue
to live the entire life of your app if the GC never runs. So, in a
nutshell, it means you don't know when the object will be freed from
memory.
 
C

Cor Ligthert [MVP]

Ben,

Did you ever thought of using a component as a template for your classes, if
you are in doubt of this kind of problems.

It works almost the same as a form template and has direct all code for the
Idisposable.

Just an idea

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