|
#1
|
|
|
|
|
hi,
To destroy objects in vb6 we used to write Set MyObj = Nothing But how do i destroy objects to clean up memory in vb.net 2005 MyObj.Dispose is that a right code or i still need to use Set MyObj = Nothing ? Set MyObj = Nothing MyObj.Dispose thank you, abhishek |
|
|
|
#2
|
|
|
|
|
"Abhishek" <nomail> kirjoitti viestissä
news:4072 > is that a right code or i still need to use Set MyObj = Nothing ? > > Set MyObj = Nothing > MyObj.Dispose No, that's totally unnecessary. Setting variable to nothing does not dispose the object, Dispose method does it. So, just call object's Dispose method or use it inside Using block. -Teemu |
|
#3
|
|
|
|
|
As soon as an object is out of scope it can be collected by the GC , this
can even happen in method scope level that is why it is in my opinion it is good coding practice to use using staments for anny object that exposes the idisposable interface and to implement this interface in custom objects. So general rule of thumb call dispose or use a using statement if the object exposes this method , setting to Nothing is not necesary although the opinions of the pro`s and con`s are wide spread , i have seen Balena code wich does set objects to Nothing after calling dispose , but i have also read articles of people who claim that it actually hurts the GC Behavior . My personal coding style is that of the dispose pattern and and relying on ,managed scope level dispose of objects , this seems to work fine for me HTH Michel Posseth "Abhishek" <nomail> schreef in bericht news:4072 [..] |
|
#4
|
|
|
|
|
Abhishek wrote:
> hi, > > To destroy objects in vb6 we used to write > > Set MyObj = Nothing > > But how do i destroy objects to clean up memory in vb.net 2005 > > MyObj.Dispose > > is that a right code or i still need to use Set MyObj = Nothing ? > > Set MyObj = Nothing > MyObj.Dispose If you want do clean up ressources ASAP you have to call Dispose and set a reference to Nothing ASAP. Though, setting a local variable to Nothing right before the end of the function, does not make sense. Call Dispose only of you know that the object will not be used anymore. For example, it's not valid to call e.graphics.dispose in a control's Paint event. Armin |
|
#5
|
|
|
|
|
here so more info for the vb6 coder
http://www.dotnet2themax.com/blogs/f...77417ed1f.aspx I like the shown generic routine in the above link verry much although it does set the object to Nothing after the call to Dispose i guess that the writer of the official MS Press Core reference guides VB pre .Net and VB.Net versions knows what he is doing . Stephany Young wrote once an answer here in the Groups regarding this topic that i like verry much <<<< You will get as many different answers as ther are programmers, however, the short answer is No. Now let me qualify that. No, you are not required to assign Nothing to an object to 'destroy' it. In general, when an object goes 'out of scope' it will become available for garbage collection and the Garbage Collector will 'clean it up' when it gets around to it. There are some classes in the Framework where you MUST 'destroy' an instance of it yourself. Off the top of my head, the WebRequest class is one. Some classes expose a Dispose method. This indicates that an instance of the class may make use of some unmanaged resources that need to be cleaned up. For such objects you need to call the Dispose method of the object rather than assigning Nothing to the object. My recommendation is to NOT explicity 'destroy' objects at this stage of your learning curve. As you progress you will soon learn which objects you need to 'destroy' whether it be by assigning Nothing or calling the Dispose method. That said, you are free to explicity 'destroy' objects, a'la VB6, if you so choose. >>>> In short what she is saying is that for your own ease of mind do not do it as it might confuse you and just let the framework handle it for you in the whole Noting VS not to Nothing battle i think it is a good and valid answer My personal opinion is that it doesn`t hurt but that it also doesn`t give you a benefit in doing so in most situations it just adds a lot of confusion and in only rare cases you might find a construction where it is necesary but you might think if the whole construction is written thoroughly, having said so this is where the nothing can come perfectly valid in its use ( when third party coders messed things up ) regards Michel Posseth "Michel Posseth [MCP]" <MSDN> schreef in bericht news:1184 [..] |
|
#6
|
|
|
|
|
"Abhishek" <nomail> wrote in message
news:4072 > hi, > > To destroy objects in vb6 we used to write > > Set MyObj = Nothing > > But how do i destroy objects to clean up memory in vb.net 2005 > > MyObj.Dispose > > is that a right code or i still need to use Set MyObj = Nothing ? > > Set MyObj = Nothing > MyObj.Dispose > > thank you, > abhishek In VB 2005/8, the only time I set an object to Nothing is when the object is a container object (list, array, collection) and it potentially contains a large number of elements (read .count > several thousand) and the method I'm using it in still has considerable processing left to do. I suspect this isn't required since the GC seems to have lexical knowledge of the code and knows when an object isn't going to referenced anymore. In general, I agree with the other posters that you should either use the "using myvar as new ...." statement to force the dispose method on you objects. For your own classes, this means you need to define them as follows <public/private/protected> class myClass implements IDisposable and fill in the code blocks for Dispose(boolean) correctly to free resources, including calling other dispose methods. Mike. |
|
#7
|
|
|
|
|
Abhishek,
There are a lot of different kind of objects although they all inherit from object, the highest object in .Net. To destroy an object you have to do nothing, it is done by the managed code (in fact one of the main addititons to VB6), this happens if an object is declared in a method as it goes out of scope, sometimes at the end of the program. In fact it will be destroyed as there is no reference anymore to it from whatever place. As long as there is a reference to it, then it will not be destroyed, how many times you use the close, the dispose methods or set its own reference to nothing. As you have a large object and you have declared it globaly, then it can make sense to set it to nothing, so the object can be destroyed before the program is at its end. To remove handles or other kind of unmaneged resources you can mostly call the method dispose, which is in 20% of the classes (by instance control) added from components which 20% of the classes is inheriting from (most control classes), and in those cases is often overridden included the handling for Idisposable. Dispose is done implicitly when an object is declared in a using clause or by most close methods by instance in the AdoNet classes. Cor "Abhishek" <nomail> wrote in message news:4072 [..] |
|
#8
|
|
|
|
|
On 2008-12-26, Armin Zingler <az.nospam> wrote:
> Abhishek wrote: >> If you want do clean up ressources ASAP you have to call Dispose and set a > reference to Nothing ASAP. Though, setting a local variable to Nothing right > before the end of the function, does not make sense. Call Dispose only of > you know that the object will not be used anymore. For example, it's not > valid to call e.graphics.dispose in a control's Paint event. > Dispose yes, setting to nothing no. As I understand it, the GC is smart enough to know if a local reference is used again in a method. If not, then even though the function is still running - the local reference will be reclaimed. In other words, setting to nothing ASP - even in the middle of a function makes little sense either. |
|
#9
|
|
|
|
|
Tom Shelton wrote:
> In other words, setting to nothing ASP - even in the middle of a > function makes little sense either. There are not only local variables. Armin |
|
#10
|
|
|
|
|
On 2008-12-26, Armin Zingler <az.nospam> wrote:
> Tom Shelton wrote: >> In other words, setting to nothing ASP - even in the middle of a >> function makes little sense either. > > There are not only local variables. That's true :) If you dealing with module or class level values, then that would be the one case where setting an object to nothing makes sense (from a memory managements standpoint). |
|
#11
|
|
|
|
|
Armin,
Why would I "ASAP set a object to Nothing". The GC does, afaik as there is not a real call to the OS, not start before the end of the method or there should be real lack of memory. I found your reply very correct, but this part gives a question to me, so I am currious to your answer? Cor "Armin Zingler" <az.nospam> wrote in message news:5108 [..] |
|
#12
|
|
|
|
|
Cor Ligthert[MVP] wrote:
> Armin, > > Why would I "ASAP set a object to Nothing". The GC does, afaik as > there is not a real call to the OS, not start before the end of the > method or there should be real lack of memory. > > I found your reply very correct, but this part gives a question to > me, so I am currious to your answer? Hi Cor, like Tom you also refer to local variables, right? Because you write "end of the method". I did not limit my statement to local variables. The only thing I wrote about local variables is that it does not make sense to set local variables to Nothing right before the end of the sub. I'm not sure if this answers your question. Armin |
|
#13
|
|
|
|
|
"Abhishek" <nomail> schrieb:
> To destroy objects in vb6 we used to write > > Set MyObj = Nothing This did not destroy the object referenced by 'MyObj' necessarily in VB6! The object has only been destroyed if there was no other reference pointing to it. |
|
#14
|
|
|
|
|
"Herfried K. Wagner [MVP]" <hirf-spam-me-here> schreef in bericht
news:3908 > "Abhishek" <nomail> schrieb: >> To destroy objects in vb6 we used to write >> >> Set MyObj = Nothing > > This did not destroy the object referenced by 'MyObj' necessarily in VB6! > The object has only been destroyed if there was no other reference > pointing to it. > > -- > M S Herfried K. Wagner > M V P <URL:http://dotnet.mvps.org/> > V B <URL:http://dotnet.mvps.org/dotnet/faqs/> AFAIK This only made sure that the reference count was decreased , this coding behavior was introduced becuase of a bug in VB 4 or 5 ( i am not sure ) cause setting to Nothing wasn`t necesary in VB6 either , however as it was considered "Good coding practice" to do so ( as it also didn`t and perhaps doesn`t ? hurt) due to this bug history , VB coders adopted this way of coding . However having said so there could still be a valid reasson to assign a Nothing / Null pointer to a object in some code constructions Just my opinion , Michel Posseth http://www.VBDotNetCoder.com |
|
#15
|
|
|
|
|
Armin,
> The only thing I wrote about local variables is that it does not make > sense to set local variables to Nothing right before the end of the sub. > I'm not sure if this answers your question. No, as you probably can see is in my message, that I wrote before your answer to Tom, the same written as in your answer to Tom. I don't see where it makes sense to set the reference of an (in the method declared) object to Nothing, that is not only for at the end. That is the part in your message that makes me curious, where can there be a reason for that. Cor "Armin Zingler" <az.nospam> wrote in message news:1532 [..] |
|
|
|
|
| Similar Threads | |
| How can I destroy objects like an ArrayList? Using Powershell v1 atm.. I'm working on a large complex script and need to be able to destro objects, specifically ArrayLists. How can I do so This is how I create... |
|
| destroy referenced objects Hello, pretend some noob has (in a fake-static class) provided the following method public static kill_object($obj) { if (!is_object($obj)) return false; |
|
| explicitly destroy Objects in PHP5 Hi, how do I "explicitly destroy" an Object in PHP5 to make sure the destructor is called an the object destroyed? unset is not an option because there are multiple... |
|
| Disposing. My objects won't destroy How do I make sure Ive disposed of my objects please? Ive added interface IDispose to a base class. Ive several sub classes inheriting from this base class. I store one or... |
|
| Learning Objects, destroy methods Hi folks, My first forrey into Perl objects sees me trying to model a railway. I've got a hash of named blocks of track, which is added to when I create a new... |
|
|
All times are GMT. The time now is 07:26 AM. | Privacy Policy
|