Re: How much do you leave to VB.Net GARBAGE COLLECTION???



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.

.



Relevant Pages

  • Re: How much do you leave to VB.Net GARBAGE COLLECTION???
    ... for object is not about net object is cleaned unmanaged reference to ... class as the most time this a method dispose as you inherited). ... resources it is of scope and it is dispose has not when enough methods, ... New object = new object as new object dim myobject myobject dim myobject ...
    (microsoft.public.dotnet.languages.vb)
  • Re: events and object lifetime
    ... onto the objects with a direct reference and had a method to set the ... Whether you use Dispose() or some other method name, ... hanging around until your application terminates and a "true" memory ... garbage collector and concentrate on programming your application. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: A re-announce on GCs defects
    ... It's bad for CPU/Resource intensive but memory cheap objects. ... There may be more than one strong references and you don't know when and where to call Dispose. ... Instead of calling the destructor you call Dispose if the reference counter is 0. ... Note GC in java and C# is not really an addictive as someone would argue since there is no way to do real memory management like delete obj in C++. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: A re-announce on GCs defects
    ... It's bad for CPU/Resource intensive but memory cheap objects. ... reference counting can take up to 50% of your ... one strong references and you don't know when and where to call Dispose. ... Programmer doesn't have to worry about memory allocation. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Few difficult for me questions about Dispose()
    ... idea) you won't achieve anything by calling Dispose. ... cleanup on classes which hold onto a lot of memory you will at least allow ... Now if I have a form that holds a reference to my object for too long, ... For example in my object persistence framework I use ...
    (microsoft.public.dotnet.languages.csharp)