473,382 Members | 1,349 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,382 software developers and data experts.

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

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.
Jul 24 '08 #1
6 1288
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" <cl**********@earthlink.netschreef in bericht
news:3s********************************@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.

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.
Jul 24 '08 #2
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
<cl**********@earthlink.netschreef in bericht
news:3s********************************@4ax.com... "alan mailer"
<cl**********@earthlink.netschreef in bericht
news:3s********************************@4ax.com... "alan mailer"
<cl***********@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.

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.
Jul 24 '08 #3
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" <cl**********@eardlink.netschreef in bericht
news:3s********************************@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.

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.
Jul 24 '08 #4
Alan Mailer 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.

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

--
Göran Andersson
_____
http://www.guffa.com
Jul 24 '08 #5
On 2008-07-24, Alan Mailer <cl**********@earthlink.netwrote:
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....

--
Tom Shelton
Jul 24 '08 #6
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).
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/
Jul 24 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
6
by: Ganesh | last post by:
Is there a utility by microsoft (or anyone) to force garbage collection in a process without have access to the process code. regards Ganesh
11
by: Rick | last post by:
Hi, My question is.. if Lisp, a 40 year old language supports garbage collection, why didn't the authors of C++ choose garbage collection for this language? Are there fundamental reasons behind...
5
by: Bob lazarchik | last post by:
Hello: We are considering developing a time critical system in C#. Our tool used in Semiconductor production and we need to be able to take meaurements at precise 10.0 ms intervals( 1000...
8
by: mike2036 | last post by:
For some reason it appears that garbage collection is releasing an object that I'm still using. The object is declared in a module and instantiated within a class that is in turn instantiated by...
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...
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 =...
350
by: Lloyd Bonafide | last post by:
I followed a link to James Kanze's web site in another thread and was surprised to read this comment by a link to a GC: "I can't imagine writing C++ without it" How many of you c.l.c++'ers use...
158
by: pushpakulkar | last post by:
Hi all, Is garbage collection possible in C++. It doesn't come as part of language support. Is there any specific reason for the same due to the way the language is designed. Or it is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...

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.