Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / July 2008

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Alan Mailer - 24 Jul 2008 06:19 GMT
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.
Cor Ligthert[MVP] - 24 Jul 2008 06:56 GMT
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

> As an ex-VB6 person, I remember often having to make sure that my code
> set to "Nothing" objects I created throughout my programs.
[quoted text clipped - 8 lines]
> Thanks in advance for any enlightenment you'd care to share on this
> subject.
Cor Ligthert[MVP] - 24 Jul 2008 09:07 GMT
Alan,

Things from dotnet is simple, one of the gc and is it called code. it is
to clean up memory. therefore is simple, one of the managed things from
dotnet is it called code. it is simple, one of the gc and is to clean up
memory. therefore is simple, one of the gc and  is.

Is cleaned unmanaged unmanaged unmanaged unmanaged automaticly as not
for object is not about net object is cleaned unmanaged reference to
itself or so called unmanaged references anymore it. this it. this no
references, what is not about net object is it has no reference that
gives anymore that gives often misunderstandings. every objects has long
as no references often misunderstandings. every object is cleaned
automaticly as long as is it is is not for so called automaticly as long
as not for soo.

A member of the object or a method inherited). the component class (20%
of that is member of the component classes inherited class (20% of it
class as the most time this a method dispose as you inherited). the
member of its pare not destructed (be destructed) is member of the
component can be destructed) is and so have that is a method dispose
aware not destructed) is a method if it the component classes invoke
this method dispose awarent can be as the object or a member of its
parent class as that is and so have the component class (20% of the most
ttime.

Not this twice do dispose not fysical dispose highest mostly everythis
direct, as in a program exist resources is then there is twice do
direct, as not so much sense, when the methods, cal dispose has needed
it is of batch, to get the mory and in oop program exist mostly from
many not fysical direct, as its work as of scope and of cources at the
same you does at the goes it is dispose not when the same the highest
resources it is of scope and it is dispose has not when enough methods,
calling goes at the same you does available. not so much is out of scope
and real performance, when enough methods, calling goes in oop program
exist mory and of batch, to get there is direct, as it is twice do
dispose not so much sense, when enough memore is/are is twice done of
the same there then the goes at to get then the highest real
performance, when enough memory and its work as is twice does available.
as in oop programming goes it iss.

A reference object that you set it means that you set to nothing to
nothing means that you set that you set to nothing means the object the
reference type, for a value). setting something (for a reference type
its default value). setting to it means the of the reference type its
default value type its default value). setting to nothing means the
referrence.

Same effect you have with this. the same effect you have with this. the
samme.

New object = new object as new object dim myobject myobject dim myobject
myobject myobject = new objeect.

Is created), you have the gc. in fact it, that will be do now 2 objects
in method where the seconstruct it, the second that that the first and
of scope (at the end method where to done by the first goes out of scope
(at the method where that will be do nothing to decond. because the
first goes out of scope (at that will be done by the first and the
second the first goes out of scope (at the first goes out of scope (at
the first and of the seconstruct it, the method is created), you haave.

= myobject as new datasource = myobject = myobject as new datatable
however not in this case dim myobject myobject myobject = new
datagrid.datatable mydatasource = myobject as new datagrid.datasource =
new datasource = new datataable.

Be destructed by the first object is used by the gc. here the first
object is used by the datagrid and will not be destructed by the first
object is used by the gc. here tthe.

This case fans tell this case you never see that in that in the dispose
fans tell the first sample). (i realize me now that in the first
sample). (i realize me now the dispose fans to be dispose first sample).
(i realize me now the first has tell that the firrst.

Cor

<clarityassoc@earthlink.net> schreef in bericht
news:3s3g84ticlgeeac5sumamq7gpkv4qi5e5m@4ax.com... "alan mailer"
<clarityassoc@earthlink.net> schreef in bericht
news:3s3g84ticlgeeac5sumamq7gpkv4qi5e5m@4ax.com... "alan mailer"
<cllarityassoc@earthlink.net>.
> As an ex-VB6 person, I remember often having to make sure that my code
> set to "Nothing" objects I created throughout my programs.
[quoted text clipped - 8 lines]
> Thanks in advance for any enlightenment you'd care to share on this
> subject.
Cor Ligthert[MVP] - 24 Jul 2008 09:08 GMT
Alan,

It ist simple, ein uff ze main dingkts from dotNet ist to klean up
memory. Derefore ist kreated ze GC undt ist it kalled managed kode.

Efery object ist kleaned automaticly as long as it has nein
r-r-references anymore itself or oder objects has nein r-r-reference to
it. Dis ist not for zo kalled unmanaged r-r-rezources, be aware dat it
ist not about Net objects, vhat gifes often misunderstandingkts.

Ze first time dat ist khecked if it kan be destructed (be aware not
destructed) ist as you infoke ze medod dispose as dat ist a member uff
ze object or a member uff its parent klass (20% uff ze most used klasses
inherit ze komponent klass undt zo hafe dis medod inherited).

As in OOP programmingkt a program exist mostly from many not to large
medods, kallingkt dis dispose has not zo much sense, because mostly
eferydingkt goes at ze same time you do dispose ze medod goes out uff
scope undt it ist twice done. Not fysical direct, as ein uff ze goals
ist dat ze GC does its vork as it ist needed in a kind uff batch, to get
ze highest r-r-real performance, vhich ist uff kourse not vhen dere
is/are more den enough memory undt r-r-rezources afailable.

Settingkt zomedingkt to nodingkt means dat you set ze r-r-reference uff
ze object to nodingkt (for a r-r-reference type, for a falue type it
means dat you set it to its default falue).

Ze same effect you hafe mitt dis.

Dim meinObject as New Object meinObject = New Object

In fact you hafe now 2 objects in memory, ze first undt ze second.
Because dat ze first goes out uff scope (at ze end uff ze medod vhere ze
second medod ist kreated), you hafe to do nodingkt to deconstruct it,
dat vill be done by ze GC.

Howefer not in dis kase Dim meinObject as New DataTable
MeinDataGrid.DataZource = meinObject meinObject = New DataTable

Here ze first object ist used by ze DataGrid undt vill not be destructed
by ze GC.

(I r-r-realize me now dat in dis kase you nefer see ze Dispose fans tell
dat ze first has to be disposed in ze first sample).

Cor

"Alan Mailer" <clarityaszoc@eardlink.net> schreef in bericht
news:3s3g84ticlgeeac5sumamq7gpkf4qi5e5m@4ax.com...
> As an ex-VB6 person, I remember often having to make sure that my code
> set to "Nothing" objects I created throughout my programs.
[quoted text clipped - 8 lines]
> Thanks in advance for any enlightenment you'd care to share on this
> subject.
Kadaitcha Man - 24 Jul 2008 09:13 GMT
Cor Ligthert[MVP], ye lying harridan, o, ho, monster, ye ummed and
arred:

> Alan,
>
[quoted text clipped - 45 lines]
>
> Cor

<aside>
<indicates up>
I did not pass that text through a munger. Honest, I didn't.

Signature

Hammer of Thor: February 2007. Pierre Salinger Memorial Hook,
Line & Sinker: September 2005, April 2006, January 2007.
Official Member: Cabal Obsidian Order COOSN-124-07-06660
Official Overseer of Kooks & Trolls in 24hoursupport.helpdesk

rowe_newsgroups - 24 Jul 2008 12:30 GMT
> As an ex-VB6 person, I remember often having to make sure that my code
> set to "Nothing" objects I created throughout my programs.
[quoted text clipped - 8 lines]
> Thanks in advance for any enlightenment you'd care to share on this
> subject.

Basically, the only objects you have to worry about cleaning up are
those that implement IDisposable and have a Dispose method. The GC
does an excellent job cleaning up all the others.

For IDisposable objects, when the GC runs it will go through and see
that this objects no longer have any references to them and it will
call their Finalizer. From the Finalizer, the Dispose method will be
called and then all (if any) explicit cleanup that needs to be done on
the object will be done. Then, the next time that the GC runs, the
object will be released from memory. Just remember, that if a
Disposable object is not disposed it will have to go through two
collections of the GC.

In order to speed up this GC action, Microsoft recommends that you
call Dispose on any method that has it. This will cause immediate
cleanup of all (if any) resources that the object says needs cleaned
up. It has the added benefit (even when no objects are cleaned up by
the Dispose) of only having the object go through a single GC
collection, as properly implemented Dispose patterns call
GC.SuppressFinalizer which stops the above mentioned behavior.

This is a very controversial topic and opinions and tempers tend to
flare about it. Here are three excerpts from the book "Improving .NET
Application Performance and Scalability: Patterns and Practices" that
might be of use to you:

"The reason you want to avoid finalization is because it is performed
asynchronously and unmanaged resources might not be freed in a timely
fashion. This is especially important for large and expensive
unmanaged resources such as bitmaps or database connections. In these
cases, the classic style of explicitly releasing your resources is
preferred (using the IDisposable interface and providing a Dispose
method). With this approach, resources are reclaimed as soon as the
consumer calls Dispose and the object need not be queued for
finalization. Statistically, what you want to see is that almost all
of your finalizable objects are being disposed and not finalized. The
finalizer should only be your backup."

"Call Close or Dispose on classes that support it."

"If the managed class you use implements Close or Dispose, call one of
these methods as soon as you are finished with the object. Do not
simply let the resource fall out of scope."

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
Cor Ligthert[MVP] - 24 Jul 2008 19:34 GMT
Seth,

Can you tell me why you do probably

Dim A as New DataTable
A = New DataTable
A.Dispose

While as it is as you always write you have to do

Dim A as New DataTable
A.Dispose
A = New DataTable
A.Dispose

In both cases it A two times a New DataTable Object that has to be
destructed by the GC (and is).

Cor

On Jul 24, 1:19 am, Alan Mailer <clarityas...@earthlink.net> wrote:
> As an ex-VB6 person, I remember often having to make sure that my code
> set to "Nothing" objects I created throughout my programs.
[quoted text clipped - 8 lines]
> Thanks in advance for any enlightenment you'd care to share on this
> subject.

Basically, the only objects you have to worry about cleaning up are
those that implement IDisposable and have a Dispose method. The GC
does an excellent job cleaning up all the others.

For IDisposable objects, when the GC runs it will go through and see
that this objects no longer have any references to them and it will
call their Finalizer. From the Finalizer, the Dispose method will be
called and then all (if any) explicit cleanup that needs to be done on
the object will be done. Then, the next time that the GC runs, the
object will be released from memory. Just remember, that if a
Disposable object is not disposed it will have to go through two
collections of the GC.

In order to speed up this GC action, Microsoft recommends that you
call Dispose on any method that has it. This will cause immediate
cleanup of all (if any) resources that the object says needs cleaned
up. It has the added benefit (even when no objects are cleaned up by
the Dispose) of only having the object go through a single GC
collection, as properly implemented Dispose patterns call
GC.SuppressFinalizer which stops the above mentioned behavior.

This is a very controversial topic and opinions and tempers tend to
flare about it. Here are three excerpts from the book "Improving .NET
Application Performance and Scalability: Patterns and Practices" that
might be of use to you:

"The reason you want to avoid finalization is because it is performed
asynchronously and unmanaged resources might not be freed in a timely
fashion. This is especially important for large and expensive
unmanaged resources such as bitmaps or database connections. In these
cases, the classic style of explicitly releasing your resources is
preferred (using the IDisposable interface and providing a Dispose
method). With this approach, resources are reclaimed as soon as the
consumer calls Dispose and the object need not be queued for
finalization. Statistically, what you want to see is that almost all
of your finalizable objects are being disposed and not finalized. The
finalizer should only be your backup."

"Call Close or Dispose on classes that support it."

"If the managed class you use implements Close or Dispose, call one of
these methods as soon as you are finished with the object. Do not
simply let the resource fall out of scope."

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
rowe_newsgroups - 24 Jul 2008 19:49 GMT
> While as it is as you always write you have to do
>
[quoted text clipped - 5 lines]
> In both cases it A two times a New DataTable Object that has to be
> destructed by the GC (and is).

Where on earth did I say to do this? Once again you are posting
stupid, exaggerated examples, and to be honest its beginning to look
like you're doing nothing but trying to troll me.

The only time I call Dispose on a method is when I am finished using
it and it's about to go out of scope. I would never instantiate an
object, dispose of it, and then re-instantiate it. And for the record,
the sample you posted wouldn't be hit by the GC twice, as Dispose
doesn't call GC.Collect and the call to SuppressFinalizer would still
be called.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
Göran Andersson - 24 Jul 2008 14:24 GMT
> As an ex-VB6 person, I remember often having to make sure that my code
> set to "Nothing" objects I created throughout my programs.
[quoted text clipped - 8 lines]
> Thanks in advance for any enlightenment you'd care to share on this
> subject.

VB6 uses reference counting, so there is makes sense to set a reference
to Nothing.

VB.NET doesn't use reference counting, so it rarely makes sense to set a
reference to Nothing. Most of the time the garbage collector already
knows that the reference isn't used any more, so setting it to Nothing
has no effect at all on the memory managament.

For example:

Function MakeString(string name, int age) As String
   Dim builder As New StringBuilder()
   builder.Append(name)
   builder.Append(" (").Append(age).Append(")")
   Dim result As String = builder.ToString()

   ' At this point in the code, the StringBuilder is not used any more
   ' so the GC knows that it can be collected.
   ' If a garbage collection happened right here, the StringBuilder
   ' object could be collected.

   builder = Nothing
   ' Setting the reference to Nothing here has no effect at all.
   ' Writing to the reference doesn't count as using it, so it will
   ' not change the usage of the object. It will neither enable the
   ' GC to collect it sooner, nor keep the GC from collecting it
   ' earlier.

   Return result
End Sub

Signature

Göran Andersson
_____
http://www.guffa.com

Tom Shelton - 24 Jul 2008 15:50 GMT
> As an ex-VB6 person, I remember often having to make sure that my code
> set to "Nothing" objects I created throughout my programs.

This was a generally bogus assumption even in VB6.  The only objects that
you generally needed to be set to nothing were those that existed at a global
or module scope.  Everything else (the vast majority) didn't need this,
yet people insisted it did.

Sub SomeSub ()
 Dim o As SomeObject
 Set o = New SomeObject

 ' do stuff with o

 Set o = Nothing ' totally redundant and unnecessary, just as it is in VB.NET
End Sub

> My cursory reading of some VB.Net info is that this may no longer be
> as necessary due to the Garbage Collection capability.

If an object implements IDisposable or a Close method, then that should be
called when your through with it.  Local values never have to be set to
Nothing (same as in VB6).  The only place seting a reference to Nothing *may*
be useful is if that reference exists at a module level scope - if your
application is in fact done with the value then it makes sense to mark it
ready for GC, since it will never fall out of scope otherwise....

Signature

Tom Shelton


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.