12 Replies - 37733 Views - Last Post: 06 June 2012 - 07:49 PM

#1 shadachi  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 25-January 08

Question : Do you still use 'Set Object = Nothing' in VB.NET?

Post icon  Posted 28 February 2012 - 07:52 PM

Hi all , I know that in VB6 , people use Set Object = Nothing but is it necessary for us to explicitly set objects to nothing in Vb.NET

Just wanted to know that is it still a common practice to set an object to nothing in VB.NET ? Or it is better off to implement IDisposable for all your classes and let GC auto manage it for you ?

I tend to always write codes like below , as i thought that by just disposing it , it is still available in memory but by setting it to nothing , it will be released . Correct me if I am wrong .. I just want to keep on learning and practice good programming . =)


Dim x As New Object()

x.Dispose()

x = nothing 


This post has been edited by AdamSpeight2008: 28 February 2012 - 08:09 PM


Is This A Good Question/Topic? 2
  • +

Replies To: Question : Do you still use 'Set Object = Nothing' in VB.NET?

#2 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,972
  • Joined: 08-June 10

Re: Question : Do you still use 'Set Object = Nothing' in VB.NET?

Posted 28 February 2012 - 11:08 PM

Setting things to null/Nothing doesn't remove the memory used by the instance of the object. Neither does Dispose. The GC does, when it runs. Remember, when you have a reference variable, you have a pointer to an object. When you repoint that variable to a null address, it doesn't delete the data at the previous address, nor does it free the memory.

The GC handles abandoned references. When the GC makes a pass, and there are no references to a block of memory, it reclaims it. So, if your application is keeping a variable alive but you want to allow the memory of what it was pointing at to be reclaimed, then setting it to Nothing abandons the memory, assuming nothing else is also pointing at it (which is entirely possible in OOP). On the other hand, in an instance method, local variables don't need to be cleared, since they'll go out of scope when the method ends, and that will also orphan the memory.

Also, read up on the way Dispose works as well.

Quote

Use this method to close or release unmanaged resources such as files, streams, and handles held by an instance of the class that implements this interface. By convention, this method is used for all tasks associated with freeing resources held by an object, or preparing an object for reuse.

...

Because the Dispose method must be called explicitly, objects that implement IDisposable must also implement a finalizer to handle freeing resources when Dispose is not called. By default, the garbage collector automatically calls an object's finalizer prior to reclaiming its memory. However, once the Dispose method has been called, it is typically unnecessary for the garbage collector to call the disposed object's finalizer. To prevent automatic finalization, Dispose implementations can call the GC.SuppressFinalize method.

For more information on implementing finalizers and the Dispose method, see the GC class, the Object.Finalize method, and Implementing Finalize and Dispose to Clean Up Unmanaged Resources.


Basically, Dispose is useful for preparing your objects for being destroyed, but it doesn't actually release the memory used by the object. If you're dealing with resources that need cleaning up, like open streams, database connections, things like that, dispose is great. I've also found it useful for things that need a block scope. For instance, I made a class that will use Windows Impersonation to run as a different user, and revert the impersonation when disposed. So you can make a using block to run code inside in an impersonated context.

But Dispose doesn't call the GC. It's a tool for you to clean an object in preparation for reclamation, not a tool for reclamation itself.

Memory management in .NET is mostly hands-off. There are ways to force or suppress collection, but you don't have to deal with it like C++ people do.
Was This Post Helpful? 2
  • +
  • -

#3 shadachi  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 25-January 08

Re: Question : Do you still use 'Set Object = Nothing' in VB.NET?

Posted 29 February 2012 - 12:34 AM

Hi Curtis , Thx for the wonderful reply and information. I am beginning to understand more and more .


Correct me if i am wrong , by my understanding after reading your reply . Sorry for the long post , I come from a C++ background , where destructors are a must . I am just a wee bit curious when it comes to new knowledge understanding . =D


1) When you reference a variable you create a pointer to an object which is equivalent to pointing to the object's memory address, yes ?

2)When you repoint that variable to a null address . I am assuming you meant setting the referenced object to null which only sets the referenced object's memory address to null but the data that was in the previous address is still in the memory. The GC handles abandoned references which will reclaim the earlier abandoned data , yes ?

3) You mentioned instance method . Do you mean by one of the instanced object's method ?

4) Local variables don't need to be cleared since they will go out of scope when the method ends . Do you mean local variables such as string , integer , etc and not instanced objects like arraylist , stringbuilder , etc where the keyword New() is used?

5) Based on the Dispose works quote. Dispose method must be called explicitly, objects that implement IDisposable must also implement a finalizer to handle freeing resources when Dispose is not called. So if i have many sub-objects within one object . Do i need to write the clearing of objects function in both Finalize() and Dispose() method just in case if dispose method was not explicitly called. ( To prevent memory leaks , just in case )

6) Can i assume that by setting an object to nothing is safe to use if the object does not have a Dispose() function.

7) As you said , Dispose doesn't call the GC. It's a tool for you to clean an object in preparation for reclamation, not a tool for reclamation itself.

So by implementing a Dispose() method in your class , you are managing and defining your own object cleanup within , right ? So how do you actually clean the instanced object that u instanced earlier ? Just leave it and the object would die by itself ( assuming that you don't reuse the object) and the GC would pick it up after a few passes Or by setting it to a null address so that the GC would be pick it up after one pass ? By doing that the stray data in the previous address would die off and gets picked up by the GC as well?

This post has been edited by shadachi: 29 February 2012 - 12:36 AM

Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1062
  • View blog
  • Posts: 4,138
  • Joined: 02-July 08

Re: Question : Do you still use 'Set Object = Nothing' in VB.NET?

Posted 01 March 2012 - 08:01 AM

In .Net the framework is so good at GC that most classes you make do not need the Dispose implementation. There are some article, if I can find them, where they states COM objects should use the dispose method for object clean up. Any class you make where you use the following; Fonts, Brushes, Pens, Graphics objects, Bitmaps(especially - very sticky memory objects).
Was This Post Helpful? 0
  • +
  • -

#5 shadachi  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 25-January 08

Re: Question : Do you still use 'Set Object = Nothing' in VB.NET?

Posted 01 March 2012 - 04:23 PM

Correct me if i am wrong ,you mentioned the GC manages all.

So is it safe for you not to dispose the object of your classes unless you are using Com Objects .

If there are multiple sub objects which are not Com objects within one single object , do I need to clean them ?

Besides that , is there a need anymore to set the referenced object's address to null by


Object = nothing 



This post has been edited by shadachi: 01 March 2012 - 04:25 PM

Was This Post Helpful? 0
  • +
  • -

#6 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1062
  • View blog
  • Posts: 4,138
  • Joined: 02-July 08

Re: Question : Do you still use 'Set Object = Nothing' in VB.NET?

Posted 01 March 2012 - 08:43 PM

Yes, that is what MSDN says. I just added the other objects that are known to have leaky tendencies. Depending on your usage you dispose of them locally or if your loading an class with object, like the ones above, and the user fills them the dispose method is a great way to protect them from themselves - sort of(they can still forget to use it)! Simple classes don't need this added protection.

'locally disposed
Private Sub DrawALine()
  Using p As New Pen(Color.Blue, 2)
    'use it
  End Using 'disposed
End Sub

Was This Post Helpful? 0
  • +
  • -

#7 shadachi  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 25-January 08

Re: Question : Do you still use 'Set Object = Nothing' in VB.NET?

Posted 01 March 2012 - 10:56 PM

Yes simple classes if u use "using" it will auto dispose the object after using.

For my case , i have heavy loaded objects in a list that would be passed around for processing and when the processing finishes or the process is terminated half way , i would need to clean up the data by disposing it .

@Heavy loaded object which means many sub objects with data within one single object.

By directly setting it to nothing , i can explicitly abandon the data allowing it to be strayed so that the GC would directly pick it up faster than waiting it to die by itself.
Was This Post Helpful? 0
  • +
  • -

#8 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1062
  • View blog
  • Posts: 4,138
  • Joined: 02-July 08

Re: Question : Do you still use 'Set Object = Nothing' in VB.NET?

Posted 01 March 2012 - 11:30 PM

You can explicitly dispose (obj = nothing) of any object that concerns you - most likely good practice. There can be performance costs by calling GC.Collect. Datasets and DataTables are also objects that should be disposed of - not sure if that's what you mean by data. Also Removing delegates you put into place (Addhandler statements) at runtime.
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: Question : Do you still use 'Set Object = Nothing' in VB.NET?

Posted 01 March 2012 - 11:39 PM

obj = nothing is not the same as obj.Dispose .
What is doing depending on the context it is used,
is Nulling out the reference or value.
It doesn't release OS resource handles, or attached events
or comparing to default value of the type.

Remember Nothing stands default value of the type.

This post has been edited by AdamSpeight2008: 01 March 2012 - 11:45 PM

Was This Post Helpful? 0
  • +
  • -

#10 shadachi  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 25-January 08

Re: Question : Do you still use 'Set Object = Nothing' in VB.NET?

Posted 03 March 2012 - 03:05 AM

@Hawk , data which means objects with filled properties . The properties hold all the values for example a transaction's details .

@Adam , I see , so explicitly setting it to nothing is just defaulting the variable's value of the type but it still is in the memory . So how do you normally dispose objects ? I usually do the following to dispose objects stored in an arraylist.


For i as integer = 0 To arraylist.count - 1

arraylist.item(i) = nothing

Next




I store objects with filled properties in the arraylist . For example an arraylist of 10 Transactions. Each transactions hold a list of properties with values.

I can't use Clear() method as it only clears the linking of the objects in arraylist but not actually clearing the object that was in it.
Was This Post Helpful? 0
  • +
  • -

#11 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: Question : Do you still use 'Set Object = Nothing' in VB.NET?

Posted 03 March 2012 - 05:03 AM

You don't the .net GC handles. From a C++ view its like C++11 Shared References, once all of the reference to said object have gone it marks it being able to be reused.

Watch the following video for more info.

E2E: Erik Meijer and Patrick Dussud - Inside Garbage Collection
Was This Post Helpful? 0
  • +
  • -

#12 shadachi  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 25-January 08

Re: Question : Do you still use 'Set Object = Nothing' in VB.NET?

Posted 03 March 2012 - 09:28 PM

@Adam , Thx for the video link . So if i am not mistaken , it is best to not explicitly set an object to nothing in Vb.NET . Yes it is the correct way in VB6 but when you set an object to nothing in vb.net you risk of making the the object or variables with no more references to it . It might be possible that you're also making it alive and prevents the object from getting collected by the GC as early as possible .

Setting it back to default values manually also creates a performance overhead , it is best to let the GC do its magic . Please correct me if i am wrong in any way .. =) btw i am quite a slow learner when it comes to this , so please bear with me .. >.<

This post has been edited by shadachi: 03 March 2012 - 09:38 PM

Was This Post Helpful? 0
  • +
  • -

#13 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Question : Do you still use 'Set Object = Nothing' in VB.NET?

Posted 06 June 2012 - 07:49 PM

I rarely use it in VB6 either. This is an excellent explanation of when to use it, which doesn't usually come up.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1