garabe 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:
MainMenu.DefInstance.Dispose(True)

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

Thanks.
 
T

Tom Shelton

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?

In the general case, I would say no that is not correct. There are
situations where explict calls to the GC can be benificial. But, for
the most part it should be avoided. Let the garbage collector do it's
job.

Now there are instances were you may have classes that use OS handles or
resources that need to be cleaned up explicitly. These are the
"unmanaged" resources you may hear about. Here is one link to an
article that talks a little more about Dispose and it's use:

http://msdn.microsoft.com/library/d...guide/html/cpconimplementingdisposemethod.asp

HTH
 
A

AlexS

If you run a tight loop creating and processing lots of objects you might
need to help GC by calling Dispose on removed objects.
But before doing so, profile your app and look at objects which survive GC
cycles.

By itself GC.Collect might not help at all.

HTH
Alex
 
G

Guest

just curious, so when I do:

me.close()
does that automatically invoke dispose method or I would still need to call:
me.dispose(true)
when closing my form?

thanks.
 
T

Tom Shelton

just curious, so when I do:

me.close()
does that automatically invoke dispose method or I would still need to call:
me.dispose(true)
when closing my form?

Me.Close should be sufficient.
 
C

Chris Dunaway

That depends on how the form was shown in the first place. If you
showed the form using ShowDialog, then you need to call Dispose. If
you showed using just Show, then Dispose is called automatically.
 

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