473,395 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

garbage collection method

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
5 5472

"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Peter Kwan | last post by:
Hi, I believe I have discovered a bug in Python 2.3. Could anyone suggest a get around? When I tested my existing Python code with the newly released Python 2.3, I get the following warning: ...
2
by: James S | last post by:
Hi, Basically I've been fighting with this code for a few days now and can't seem to work around this problem. Included is the output, the program I use to get this error and the source code for...
1
by: Bob | last post by:
Are there any known applications out there used to test the performance of the .NET garbage collector over a long period of time? Basically I need an application that creates objects, uses them, and...
2
by: C P | last post by:
I'm coming from Delphi where I have to explicitly create and destroy instances of objects. I've been working through a C#/ASP.NET book, and many of the examples repeat the same SqlConnection,...
8
by: Martin Maat | last post by:
I am puzzled. I have this object that uses a thread. The thread is encapsulated by the object, the object has Start and Stop methods to enable the client to start or stop the thread. I found...
4
by: bkazlak | last post by:
Hello, I have a quick question might help me understand garbage collection. let's say I'm having a static collection of objects in one class, so this collection should be cached and present...
36
by: André | last post by:
Hello, I have a c# application that creates a timer to fire every minute using multithreading. On every minute it calls a Ping class that I made using sockets. It creates a new ping class then...
28
by: Goalie_Ca | last post by:
I have been reading (or at least googling) about the potential addition of optional garbage collection to C++0x. There are numerous myths and whatnot with very little detailed information. Will...
8
by: Paul.Lee.1971 | last post by:
Hi everyone, A program that I'm helping to code seems to slow down drastically during initialisation, and looking at the profiling graph, it seems to be the garbage collector thats slowing things...
56
by: Johnny E. Jensen | last post by:
Hellow I'am not sure what to think about the Garbage Collector. I have a Class OutlookObject, It have two private variables. Private Microsoft.Office.Interop.Outlook.Application _Application =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.