Re: How much do you leave to VB.Net GARBAGE COLLECTION???
- From: "Cor Ligthert[MVP]" <notmyfirstname@xxxxxxxxx>
- Date: Thu, 24 Jul 2008 07:56:08 +0200
Alan,
It is simple, one of the main things from dotNet is to clean up memory. Therefore is created the GC and is it called managed code.
Every object is cleaned automaticly as long as it has no references anymore itself or other objects has no reference to it. This is not for so called unmanaged resources, be aware that it is not about Net objects, what gives often misunderstandings.
The first time that is checked if it can be destructed (be aware not destructed) is as you invoke the method dispose as that is a member of the object or a member of its parent class (20% of the most used classes inherit the component class and so have this method inherited).
As in OOP programming a program exist mostly from many not to large methods, calling this dispose has not so much sense, because mostly everything goes at the same time you do dispose the method goes out of scope and it is twice done. Not fysical direct, as one of the goals is that the GC does its work as it is needed in a kind of batch, to get the highest real performance, which is of course not when there is/are more then enough memory and resources available.
Setting something to nothing means that you set the reference of the object to nothing (for a reference type, for a value type it means that you set it to its default value).
The same effect you have with this.
Dim myObject as New Object
myObject = New Object
In fact you have now 2 objects in memory, the first and the second.
Because that the first goes out of scope (at the end of the method where the second method is created), you have to do nothing to deconstruct it, that will be done by the GC.
However not in this case
Dim myObject as New DataTable
MyDataGrid.DataSource = myObject
myObject = New DataTable
Here the first object is used by the DataGrid and will not be destructed by the GC.
(I realize me now that in this case you never see the Dispose fans tell that the first has to be disposed in the first sample).
Cor
"Alan Mailer" <clarityassoc@xxxxxxxxxxxxx> schreef in bericht news:3s3g84ticlgeeac5sumamq7gpkv4qi5e5m@xxxxxxxxxx
As an ex-VB6 person, I remember often having to make sure that my code
set to "Nothing" objects I created throughout my programs.
My cursory reading of some VB.Net info is that this may no longer be
as necessary due to the Garbage Collection capability.
I wanted to hear from you experts. I notice VB.Net has methods like
"Dispose" and such. So, if Garbage Collection works, when, if ever do
you write in code that specifically destroys objects you create?
Thanks in advance for any enlightenment you'd care to share on this
subject.
.
- Follow-Ups:
- Re: How much do you leave to VB.Net GARBAGE COLLECTION???
- From: Cor Ligthert[MVP]
- Re: How much do you leave to VB.Net GARBAGE COLLECTION???
- From: Cor Ligthert[MVP]
- Re: How much do you leave to VB.Net GARBAGE COLLECTION???
- References:
- How much do you leave to VB.Net GARBAGE COLLECTION???
- From: Alan Mailer
- How much do you leave to VB.Net GARBAGE COLLECTION???
- Prev by Date: How much do you leave to VB.Net GARBAGE COLLECTION???
- Next by Date: Re: creating a simple ftp program with logs...??
- Previous by thread: How much do you leave to VB.Net GARBAGE COLLECTION???
- Next by thread: Re: How much do you leave to VB.Net GARBAGE COLLECTION???
- Index(es):
Relevant Pages
|