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

garbage collection method

P: n/a
Ben
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.

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

P: n/a

"Ben" <Be*@discussions.microsoft.com> wrote in message
news:D0**********************************@microsof t.com...
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
Nov 21 '05 #2

P: n/a
Ben


"David Browne" wrote:

"Ben" <Be*@discussions.microsoft.com> wrote in message
news:D0**********************************@microsof t.com...
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.


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?

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.


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.
Nov 21 '05 #3

P: n/a
Ben
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?
"Ben" wrote:


"David Browne" wrote:

"Ben" <Be*@discussions.microsoft.com> wrote in message
news:D0**********************************@microsof t.com...
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.


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?

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.


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.

Nov 21 '05 #4

P: n/a
On 2005-07-27, Ben <Be*@discussions.microsoft.com> wrote:


"David Browne" wrote:

"Ben" <Be*@discussions.microsoft.com> wrote in message
news:D0**********************************@microsof t.com...
> 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.


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.

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


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.


--
Tom Shelton [MVP]
Nov 21 '05 #5

P: n/a
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
Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.