Re: Set somthing = Nothing
i think calling up some value in the db with rs(without Dim rs As New ADODB.Recordset)
should reveal that.
but i'm not sure if vb automatically set rs to nothing
Re: Set somthing = Nothing
You have to set it to Nothing. Some objects (Like WORD) will actually stay in memory even after your app ends if you don't set them to Nothing.
Re: Set somthing = Nothing
Quote:
Originally Posted by dglienna
You have to set it to Nothing. Some objects (Like WORD) will actually stay in memory even after your app ends if you don't set them to Nothing.
So does it mean that some object will be set to Nothing automatically, but some will not? We should set it Nothing every time.
Re: Set somthing = Nothing
I think objects are unloaded when the app closes (according to joacim) but not all of them. It's a good habit to get into if you always set all objects to Nothing. That way you don't have to worry about them, and your app will always end when you unload forms.
Re: Set somthing = Nothing
An object will not be set to nothing if it has other dependant objects holding a reference to it still. Also, some COM objects may still be instanciated after your program closes unless you close and destroy all their references.
I noticed that your using a recordset for an example. You should always .Close your rs and then set it to nothing.
Re: Set somthing = Nothing
I completely agree with RobDog888.
I would also like to mention an issue with the code posted - when you Dim as "New" anything, it slows down the code which uses this object, as each time you reference it VB has to check whether or not it has already been initialised (and set it if not).
Instead of this:
VB Code:
Dim rs As New ADODB.Recordset
You should use this:
VB Code:
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Re: Set somthing = Nothing
Quote:
Originally Posted by si_the_geek
I completely agree with RobDog888.
I would also like to mention an issue with the code posted - when you Dim as "New" anything, it slows down the code which uses this object, as each time you reference it VB has to check whether or not it has already been initialised (and set it if not).
Instead of this:
VB Code:
Dim rs As New ADODB.Recordset
You should use this:
VB Code:
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
I am interested in what si the geek said. I don't understand cleary.
Can you tell me the different between the following clearly(the process of the program)?
vbcode]
Dim rs As New ADODB.Recordset
[/Highlight]
And
VB Code:
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
[/QUOTE]
Re: Set somthing = Nothing
Yep, when you Dim 'as New ...' like this:
VB Code:
Dim rs As New ADODB.Recordset
..VB does a bit of 'clever' memory management, rather than create all objects at the point of definition (the Dim line), it creates them when they are first used (ie: the first line of code which uses rs).
In terms of the memory used this is very good, as you do not have lots of objects in memory which aren't being used - if you have lots of these in the General Declarations section this would be as soon as your form loads (or when the program loads if they are in modules!).
Unfortunately, in order to do this the object has to be created when it is first used - and to do this you need to check every time that it is used if it has already been created, and this checking process takes time. For objects like recordsets (which are often used hundreds of times) the slow-down effect of all of these checks is significant.
Re: Set somthing = Nothing
Maybe a habit from other programming languages but I always set an object to nothing after finishing with it to ensure there are not memory leaks from the program.
Re: Set somthing = Nothing
Actually an object is never set to Nothing. What you do is to set the object reference to Nothing, and by doing so release the memory used by that reference. In your case you use a local variable as the reference and it will fall out of scoop when the procedure ends so setting it to Nothing is not really necassary (even though I think it's a good practice to do so). VB will destroy the reference when the procedure ends which will decrease the reference count in the actual object and if it then has reached zero (no other references) the object is destroyed.
Dglienna mentioned that, for example, a Word.Application object will not be destroyed and that is true, however your reference is deleted and the reference counter is indeed decreased. But since Word is loaded out-of-process it will remain in memory unless you explicitly close the application. However that is not done by setting the reference to Nothing, it is done by calling the Quit method. So it's important to close references like database connections and recordsets but you don't close those either by setting your reference to Nothing, you do it by calling the Close method.
When an application ends VB will destroy all object references but it will not close connections or call any other methods of the object to remove out-of-process objects from memory, that is your resposibility.
So, in VB you don't have to set a reference to Nothing if you use it correctly, but it might free up some memory if you do. On the other hand if you use VBScript in a classic ASP application it is more important to set object references to Nothing since IIS will not destroy a created object if you don't. There is a difference between creating an object using Server.CreateObject compared to the regular CreateObject function. With this said I think it's best to always remember to set an object reference to Nothing so if you switch development environment you don't forget to do it when it's necassary to do so.
Also, as you use a loose bound object (you use the New keyword on the same line as the Dim statement) it really doesn't matter if you set the reference to Nothing since if you by accident would use a property or call a method with that reference variable after doing so the object is simply recreated. So always use strong bound objects as ychhuong and RobDog mentioned above.
Re: Set somthing = Nothing
Umm, that was Si, Joacim. :) But I have said it before too. :D
Just giving credit where the credit is due. :)
Re: Set somthing = Nothing
Oh yeah, sorry... You both use that ugly color on your user names :)