473,698 Members | 2,524 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1307
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 misunderstandin gs.

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.Data Source = 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**********@e arthlink.netsch reef in bericht
news:3s******** *************** *********@4ax.c om...
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 misunderstandin gs. every objects has long
as no references often misunderstandin gs. 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.datata ble mydatasource = myobject as new datagrid.dataso urce =
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**********@e arthlink.netsch reef in bericht
news:3s******** *************** *********@4ax.c om... "alan mailer"
<cl**********@e arthlink.netsch reef in bericht
news:3s******** *************** *********@4ax.c om... "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 misunderstandin gkts.

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.Da taZource = 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**********@e ardlink.netschr eef in bericht
news:3s******** *************** *********@4ax.c om...
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(stri ng name, int age) As String
Dim builder As New StringBuilder()
builder.Append( name)
builder.Append( " (").Append(age) .Append(")")
Dim result As String = builder.ToStrin g()

' 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**********@e arthlink.netwro te:
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 SuppressFinaliz er 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
2327
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 then throws them away and then monitors the garbage collection and store statistics on it, preferably in C#. I want to know what is the longest period of time that an application may lock up while garbage collection is processing. Thanks!
6
810
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
2723
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 this? Is it because C is generally a 'low level' language and they didn't want garbage collection to creep into C++ and ruin everything? Just wondering :)
5
3602
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 measurement exactly 10 ms apart. In the future this may decrease to 5ms ). I am concerned that if garbage collection invokes during this time it may interfere with our measurement results. I have looked over the garbage collection mechanism and see no...
8
3041
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 the mainline. The class that instantiated the object in question is definitely still in existence at the point garbage collection swoops in and yanks it out from under my processing. Is there a way to ensure an instantiated object cannot be freed...
28
3171
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 this work be library based or language based and will it be based on that of managed C++? Then of course there are the finer technical questions raised (especially due to pointer abuse). Is a GC for C++ just a pipe dream or is there a lot of work...
56
3689
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 = null; Private Microsoft.Office.Interop.Outlook.NameSpace _Namespace = null; The Constructor: public OutlookObject()
350
11746
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 one, and in what percentage of your projects is one used? I have never used one in personal or professional C++ programming. Am I a holdover to days gone by?
158
7812
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 discouraged due to some specific reason. If someone can give inputs on the same, it will be of great help. Regards, Pushpa
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9166
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9030
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4621
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.