|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
object = nothing?Hi all,
Coming from the good old VB6 days we were told to always destroy our objects as follows:- Dim obj as New MyObject ' do some work with obj obj = Nothing I have been doing this in .Net also for quite a while now. The other day one of my peers mentioned that this is a BIG NO NO and that I should let the Garbage Collection process handle this for me. Can someone share any insight on this? TIA! It is safe to set your objects to Nothing. They will immediately be marked
for garbage collection. If you skip the assignment to Nothing, they will still be marked for gargabe collection once they go out of scope and there are no other references to the objects. For most local variables, this occurs when you exit the function or subroutine. Some objects need a little more attention. If an object supports the IDisposable interface, it includes a Dispose method that should be called before you are finished with it. This guarantees that any acquired resources are released before garbage collection. The new Visual Basic 2005 Using keyword will help with this process. ----- Tim Patrick Start-to-Finish Visual Basic 2005 Show quoteHide quote > Hi all, > > Coming from the good old VB6 days we were told to always destroy our > objects as follows:- > > Dim obj as New MyObject > ' do some work with obj > obj = Nothing > I have been doing this in .Net also for quite a while now. The other > day one of my peers mentioned that this is a BIG NO NO and that I > should let the Garbage Collection process handle this for me. > > Can someone share any insight on this? > > TIA! > <param@community.nospam> schrieb:
> Coming from the good old VB6 days we were told to always destroy our Your peer is right and he isn't.> objects as follows:- > > Dim obj as New MyObject > ' do some work with obj > obj = Nothing > > I have been doing this in .Net also for quite a while now. The other day > one of my peers mentioned that this is a BIG NO NO and that I should let > the Garbage Collection process handle this for me. Note that COM used reference counting to detect whether or not an object should be destroyed. .NET uses a different approach. The garbage collector checks objects in certain periods of time and destroys them if necessary (this is simplified, I suggest to read the chapters about garbage collection and the garbage collector (GC) in the documentation). Thus setting a variable to 'Nothing' does not necessarily destroy the object immediately if no other references are pointing to it. Because of the evolvedness of .NET's garbage collector it is not even necessary to clear all references to an object to be able to destroy it. If two objects reference each other but are not reachable from the application's code any more, the garbage collector will detect this and will clean up the objects after some time. So the conclusion is: Setting local variables to 'Nothing' at the end of the procedure does not make any sense. This applies to VB6 too. Setting variables to 'Nothing' prior to assigning another reference to the variable does not make any sense at all too, even not in VB6. It may however make sense to set private variables or properties to 'Nothing' to speed up cleanup and maintain a valid state. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://dotnet.mvps.org/dotnet/faqs/> Herfried K. Wagner [MVP] wrote:
> Setting variables to 'Nothing' prior to assigning another reference to Actually, there is a situation in VB6 where it does make sense. Example:> the variable does not make any sense at all too, even not in VB6. Set objRs = objConnection.Execute("select something from sometable") lngValue = objRs(0) objRs.Close() Set objRs = Nothing Set objRs = objConnection.Execute("select something from someothertable") lngValue2 = objRs(0) objRs.Close() Removing the reference before creating a new recordset will return the first recordset to the object pool. When the next recordset is created, it can be reused from the object pool. If you don't remove the reference, the first recordset will not be returned to the object pool until after the second has been created. So in theory setting an object to Nothing, if not anything should help the
GC in doing its job right? Since objects will be better identified as candidates for garbage collection? Show quoteHide quote "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message news:uz9ZBqV9GHA.4712@TK2MSFTNGP03.phx.gbl... > <param@community.nospam> schrieb: >> Coming from the good old VB6 days we were told to always destroy our >> objects as follows:- >> >> Dim obj as New MyObject >> ' do some work with obj >> obj = Nothing >> >> I have been doing this in .Net also for quite a while now. The other day >> one of my peers mentioned that this is a BIG NO NO and that I should let >> the Garbage Collection process handle this for me. > > Your peer is right and he isn't. > > Note that COM used reference counting to detect whether or not an object > should be destroyed. .NET uses a different approach. The garbage > collector checks objects in certain periods of time and destroys them if > necessary (this is simplified, I suggest to read the chapters about > garbage collection and the garbage collector (GC) in the documentation). > Thus setting a variable to 'Nothing' does not necessarily destroy the > object immediately if no other references are pointing to it. > > Because of the evolvedness of .NET's garbage collector it is not even > necessary to clear all references to an object to be able to destroy it. > If two objects reference each other but are not reachable from the > application's code any more, the garbage collector will detect this and > will clean up the objects after some time. > > So the conclusion is: Setting local variables to 'Nothing' at the end of > the procedure does not make any sense. This applies to VB6 too. Setting > variables to 'Nothing' prior to assigning another reference to the > variable does not make any sense at all too, even not in VB6. It may > however make sense to set private variables or properties to 'Nothing' to > speed up cleanup and maintain a valid state. > > -- > M S Herfried K. Wagner > M V P <URL:http://dotnet.mvps.org/> > V B <URL:http://dotnet.mvps.org/dotnet/faqs/> Hi,
Yes, Herfried means that if the "obj" reference is a "local" variable, then it is not necessary to set "obj" to nothing to help GC. This is because, as a "local" variable(reference), "obj" will be go out of scope automatically. So after exiting the method, it automatically has the same effect of setting "obj" to nothing.("obj" is destroyed automatically, so it is "nothing"). Anyway, I do not think setting "obj" to nothing is a "Big NO, NO". It will not hurt performance much, but it is a good habit to "help" GC in certain situation. Let's say that you have a private field of bitmap type(note, it is not a "local" variable). If you are using this field to refer a large bitmap, after you used it, it is a really good idea to set the field to "nothing", this may help to tell GC that this large bitmap is not referenced by any references, so it can be collected once the free memory is low. So this is not a big issue, you may continue to set all the unused references to nothing. This may or may not help GC, but it will not hurt performance much. Hope this is clear. Best regards, Jeffrey Tan Microsoft Online Community Support ================================================== Get notification to my posts through email? Please refer to http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif ications. Note: The MSDN Managed Newsgroup support offering is for non-urgent issues where an initial response from the community or a Microsoft Support Engineer within 1 business day is acceptable. Please note that each follow up response may take approximately 2 business days as the support professional working with you may need further investigation to reach the most efficient resolution. The offering is not appropriate for situations that require urgent, real-time or phone-based interactions or complex project analysis and dump analysis issues. Issues of this nature are best handled working with a dedicated Microsoft Support Engineer by contacting Microsoft Customer Support Services (CSS) at http://msdn.microsoft.com/subscriptions/support/default.aspx. ================================================== This posting is provided "AS IS" with no warranties, and confers no rights. Hello ,,,
Well you have started something :-) ....... this has been discussed manny times in this group and it always ends the same :-) The below is my opinion , wich is formed by following a lot of these discussions and how it is described in the manny books i own The BIG NO NO is a bit odd , he would be right if he would have said that it isn`t necesary and that you are typing a line to much if your object is declared in method scope but there are situations thinkable where it is valid to set a object to Nothing ( however never absolut necesary , as it could have been in VB6 ) so here are some scenario`s for you to rethink : Example A Private sub Useobject () dim foo as new Objfoo.lib ---- use the foo object --- setting the foo object here to nothing is absolute not necesary --- as soon as it runs out of scope it is marked for collection end sub Example B Private sub Useobject () dim foo as new Objfoo.lib for i as integer = 0 to 1000 if i < 500 then ---- use the foo object else ---- stop using the foo object end if next end sub --- here is a situation where it is valid to set the foo object to nothing in the else statement ( if i = 500 then foo=Nothing ) --- especially when it is a resource hungry object , However it is not guaranteed that the object is collected at this point end sub Example C The object is declared in class scope ( because you use it withevents or whatever reasson you might have ) and for some reasson you can live without it , for a time and if needed you can declare a new instance i encountered this scenario myself in a remoting scenario wich used a singleton if you use VB.Net 2005 then learn yourself to use the using stetement ( it makes sure everything is nicely cleaned up , and as a bonus it makes your code nicer readable ) example Private sub Useobject () using foo as new Objfoo.lib ---- use the foo object end using end sub As said the above is my own opinion without warranty`s especially about example B i get a lot of nasty responses however it is a Balena example written in the core reference guide of VB.net,, so i guess it should be a valid situation . I know this thread is going to explode ( as it normally does on this subject ) with all people telling you something different , i would say form your own opinion by reading about the GC on MSDN then you will see what it does and what it doesn`t , and especially this is verry important , setting a object to Nothing does not mean that you will get your resources inmediatly back , however in resource hungry systems it might make a difference in the right scenario . regards Michel Posseth [MCP] <param@community.nospam> schreef in bericht Show quoteHide quote news:eGGwBDV9GHA.1172@TK2MSFTNGP03.phx.gbl... > Hi all, > > Coming from the good old VB6 days we were told to always destroy our > objects as follows:- > > Dim obj as New MyObject > ' do some work with obj > obj = Nothing > > I have been doing this in .Net also for quite a while now. The other day > one of my peers mentioned that this is a BIG NO NO and that I should let > the Garbage Collection process handle this for me. > > Can someone share any insight on this? > > TIA! > So forgive my ignorance here, but I am presuming VB 2005 comes with Visual
Studio 2005 and .net 2.0 framework right? What does the Using statement do? Can I use it for both Public and Private variable declarations? Show quoteHide quote "Michel Posseth [MCP]" <M***@posseth.com> wrote in message news:OsjqzQc9GHA.4572@TK2MSFTNGP02.phx.gbl... > Hello ,,, > > Well you have started something :-) ....... this has been discussed > manny times in this group and it always ends the same :-) > > The below is my opinion , wich is formed by following a lot of these > discussions and how it is described in the manny books i own > > The BIG NO NO is a bit odd , he would be right if he would have said > that it isn`t necesary and that you are typing a line to much > if your object is declared in method scope > > but there are situations thinkable where it is valid to set a object to > Nothing ( however never absolut necesary , as it could have been in > VB6 ) > > so here are some scenario`s for you to rethink : > > Example A > Private sub Useobject () > dim foo as new Objfoo.lib > > ---- use the foo object > > --- setting the foo object here to nothing is absolute not necesary > --- as soon as it runs out of scope it is marked for collection > end sub > > Example B > > Private sub Useobject () > dim foo as new Objfoo.lib > > for i as integer = 0 to 1000 > > if i < 500 then > ---- use the foo object > else > ---- stop using the foo object > end if > next > end sub > > --- here is a situation where it is valid to set the foo object to nothing > in the else statement ( if i = 500 then foo=Nothing ) > --- especially when it is a resource hungry object , However it is not > guaranteed that the object is collected at this point > end sub > > Example C > The object is declared in class scope ( because you use it withevents or > whatever reasson you might have ) > and for some reasson you can live without it , for a time and if needed > you can declare a new instance > i encountered this scenario myself in a remoting scenario wich used a > singleton > > > if you use VB.Net 2005 then learn yourself to use the using stetement > ( it makes sure everything is nicely cleaned up , and as a bonus it makes > your code nicer readable ) > > example > > Private sub Useobject () > using foo as new Objfoo.lib > > ---- use the foo object > > end using > end sub > > > As said the above is my own opinion without warranty`s especially about > example B i get a lot of nasty responses however it is a Balena example > written in the core reference guide of VB.net,, so i guess it should be a > valid situation . > > I know this thread is going to explode ( as it normally does on this > subject ) with all people telling you something different , i would say > form your own opinion > by reading about the GC on MSDN then you will see what it does and what > it doesn`t , and especially this is verry important , setting a object to > Nothing does not mean that you will get your resources inmediatly back , > however in resource hungry systems it might make a difference in the right > scenario . > > regards > > Michel Posseth [MCP] > > > > > > > <param@community.nospam> schreef in bericht > news:eGGwBDV9GHA.1172@TK2MSFTNGP03.phx.gbl... >> Hi all, >> >> Coming from the good old VB6 days we were told to always destroy our >> objects as follows:- >> >> Dim obj as New MyObject >> ' do some work with obj >> obj = Nothing >> >> I have been doing this in .Net also for quite a while now. The other day >> one of my peers mentioned that this is a BIG NO NO and that I should let >> the Garbage Collection process handle this for me. >> >> Can someone share any insight on this? >> >> TIA! >> > > Hello ,
Yes ,, VB 2005 comes with Visual Studio 2005 The using statement can only be used inside methods The using statement guarantees , that dispose is called after exiting the using block Public Sub setbigbold(ByVal c As Control) Using nf As New System.Drawing.Font("Arial", 12.0F, _ System.Drawing.FontStyle.Bold) c.Font = nf c.Text = "This is 12-point Arial bold" End Using End Sub for more info about the using statement http://msdn2.microsoft.com/en-us/library/htd05whh.aspx hth Michel Posseth [MCP] <param@community.nospam> schreef in bericht Show quoteHide quote news:ejo3WNg9GHA.4552@TK2MSFTNGP05.phx.gbl... > So forgive my ignorance here, but I am presuming VB 2005 comes with Visual > Studio 2005 and .net 2.0 framework right? What does the Using statement > do? Can I use it for both Public and Private variable declarations? > > > "Michel Posseth [MCP]" <M***@posseth.com> wrote in message > news:OsjqzQc9GHA.4572@TK2MSFTNGP02.phx.gbl... >> Hello ,,, >> >> Well you have started something :-) ....... this has been discussed >> manny times in this group and it always ends the same :-) >> >> The below is my opinion , wich is formed by following a lot of these >> discussions and how it is described in the manny books i own >> >> The BIG NO NO is a bit odd , he would be right if he would have said >> that it isn`t necesary and that you are typing a line to much >> if your object is declared in method scope >> >> but there are situations thinkable where it is valid to set a object to >> Nothing ( however never absolut necesary , as it could have been in >> VB6 ) >> >> so here are some scenario`s for you to rethink : >> >> Example A >> Private sub Useobject () >> dim foo as new Objfoo.lib >> >> ---- use the foo object >> >> --- setting the foo object here to nothing is absolute not necesary >> --- as soon as it runs out of scope it is marked for collection >> end sub >> >> Example B >> >> Private sub Useobject () >> dim foo as new Objfoo.lib >> >> for i as integer = 0 to 1000 >> >> if i < 500 then >> ---- use the foo object >> else >> ---- stop using the foo object >> end if >> next >> end sub >> >> --- here is a situation where it is valid to set the foo object to >> nothing in the else statement ( if i = 500 then foo=Nothing ) >> --- especially when it is a resource hungry object , However it is not >> guaranteed that the object is collected at this point >> end sub >> >> Example C >> The object is declared in class scope ( because you use it withevents or >> whatever reasson you might have ) >> and for some reasson you can live without it , for a time and if needed >> you can declare a new instance >> i encountered this scenario myself in a remoting scenario wich used a >> singleton >> >> >> if you use VB.Net 2005 then learn yourself to use the using stetement ( >> it makes sure everything is nicely cleaned up , and as a bonus it makes >> your code nicer readable ) >> >> example >> >> Private sub Useobject () >> using foo as new Objfoo.lib >> >> ---- use the foo object >> >> end using >> end sub >> >> >> As said the above is my own opinion without warranty`s especially about >> example B i get a lot of nasty responses however it is a Balena example >> written in the core reference guide of VB.net,, so i guess it should be a >> valid situation . >> >> I know this thread is going to explode ( as it normally does on this >> subject ) with all people telling you something different , i would say >> form your own opinion >> by reading about the GC on MSDN then you will see what it does and what >> it doesn`t , and especially this is verry important , setting a object to >> Nothing does not mean that you will get your resources inmediatly back , >> however in resource hungry systems it might make a difference in the >> right scenario . >> >> regards >> >> Michel Posseth [MCP] >> >> >> >> >> >> >> <param@community.nospam> schreef in bericht >> news:eGGwBDV9GHA.1172@TK2MSFTNGP03.phx.gbl... >>> Hi all, >>> >>> Coming from the good old VB6 days we were told to always destroy our >>> objects as follows:- >>> >>> Dim obj as New MyObject >>> ' do some work with obj >>> obj = Nothing >>> >>> I have been doing this in .Net also for quite a while now. The other day >>> one of my peers mentioned that this is a BIG NO NO and that I should let >>> the Garbage Collection process handle this for me. >>> >>> Can someone share any insight on this? >>> >>> TIA! >>> >> >> > > "Michel Posseth [MCP]" <M***@posseth.com> schrieb: Using 'Using' is a bad idea in the sample above because the control needs an > The using statement guarantees , that dispose is called after exiting the > using block > > Public Sub setbigbold(ByVal c As Control) > Using nf As New System.Drawing.Font("Arial", 12.0F, _ > System.Drawing.FontStyle.Bold) > > c.Font = nf > c.Text = "This is 12-point Arial bold" > End Using > End Sub undisposed font even after setting the font! -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://dotnet.mvps.org/dotnet/faqs/> Herfried ..
> Using 'Using' is a bad idea in the sample above because the control needs This example is copied and pasted from the MSDN url i provided to the TS> an undisposed font even after setting the font! so i guess someone at MS has some work to do ?? scroll to the bottom of this URL to see the example that MS provides http://msdn2.microsoft.com/en-us/library/htd05whh.aspx regards Michel Posseth [MCP] Show quoteHide quote "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> schreef in bericht news:%23if0xDh9GHA.4644@TK2MSFTNGP04.phx.gbl... > "Michel Posseth [MCP]" <M***@posseth.com> schrieb: >> The using statement guarantees , that dispose is called after exiting >> the using block >> >> Public Sub setbigbold(ByVal c As Control) >> Using nf As New System.Drawing.Font("Arial", 12.0F, _ >> System.Drawing.FontStyle.Bold) >> >> c.Font = nf >> c.Text = "This is 12-point Arial bold" >> End Using >> End Sub > > Using 'Using' is a bad idea in the sample above because the control needs > an undisposed font even after setting the font! > > -- > M S Herfried K. Wagner > M V P <URL:http://dotnet.mvps.org/> > V B <URL:http://dotnet.mvps.org/dotnet/faqs/> Herfried ,
Curious as i am i have just tested the example provided by MSDN so i threw a control in the method ,,, and ..... it worked as expected Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click setbigbold(Button1) End Sub Public Sub setbigbold(ByVal c As Control) Using nf As New System.Drawing.Font("Arial", 12.0F, _ System.Drawing.FontStyle.Bold) c.Font = nf c.Text = "This is 12-point Arial bold" End Using End Sub regards Michel Posseth [MCP] Show quoteHide quote "Michel Posseth [MCP]" wrote: > Herfried .. > > > Using 'Using' is a bad idea in the sample above because the control needs > > an undisposed font even after setting the font! > > > > This example is copied and pasted from the MSDN url i provided to the TS > > so i guess someone at MS has some work to do ?? > > scroll to the bottom of this URL to see the example that MS provides > > http://msdn2.microsoft.com/en-us/library/htd05whh.aspx > > > regards > > Michel Posseth [MCP] > > > "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> schreef in bericht > news:%23if0xDh9GHA.4644@TK2MSFTNGP04.phx.gbl... > > "Michel Posseth [MCP]" <M***@posseth.com> schrieb: > >> The using statement guarantees , that dispose is called after exiting > >> the using block > >> > >> Public Sub setbigbold(ByVal c As Control) > >> Using nf As New System.Drawing.Font("Arial", 12.0F, _ > >> System.Drawing.FontStyle.Bold) > >> > >> c.Font = nf > >> c.Text = "This is 12-point Arial bold" > >> End Using > >> End Sub > > > > Using 'Using' is a bad idea in the sample above because the control needs > > an undisposed font even after setting the font! > > > > -- > > M S Herfried K. Wagner > > M V P <URL:http://dotnet.mvps.org/> > > V B <URL:http://dotnet.mvps.org/dotnet/faqs/> > > > Michel,
What can be the reason that you set in your first exsample 2 the object privat. In both situations is their in my opinion (if you set them in your method at the end to nothing) not any reason to do that, although it seems to be a kind of obfuscating from some developers to set everything global. Cor Only the last one will stay if you don't set it to nothing. Show quoteHide quote "Michel Posseth [MCP]" <M***@posseth.com> schreef in bericht news:OsjqzQc9GHA.4572@TK2MSFTNGP02.phx.gbl... > Hello ,,, > > Well you have started something :-) ....... this has been discussed > manny times in this group and it always ends the same :-) > > The below is my opinion , wich is formed by following a lot of these > discussions and how it is described in the manny books i own > > The BIG NO NO is a bit odd , he would be right if he would have said > that it isn`t necesary and that you are typing a line to much > if your object is declared in method scope > > but there are situations thinkable where it is valid to set a object to > Nothing ( however never absolut necesary , as it could have been in > VB6 ) > > so here are some scenario`s for you to rethink : > > Example A > Private sub Useobject () > dim foo as new Objfoo.lib > > ---- use the foo object > > --- setting the foo object here to nothing is absolute not necesary > --- as soon as it runs out of scope it is marked for collection > end sub > > Example B > > Private sub Useobject () > dim foo as new Objfoo.lib > > for i as integer = 0 to 1000 > > if i < 500 then > ---- use the foo object > else > ---- stop using the foo object > end if > next > end sub > > --- here is a situation where it is valid to set the foo object to nothing > in the else statement ( if i = 500 then foo=Nothing ) > --- especially when it is a resource hungry object , However it is not > guaranteed that the object is collected at this point > end sub > > Example C > The object is declared in class scope ( because you use it withevents or > whatever reasson you might have ) > and for some reasson you can live without it , for a time and if needed > you can declare a new instance > i encountered this scenario myself in a remoting scenario wich used a > singleton > > > if you use VB.Net 2005 then learn yourself to use the using stetement > ( it makes sure everything is nicely cleaned up , and as a bonus it makes > your code nicer readable ) > > example > > Private sub Useobject () > using foo as new Objfoo.lib > > ---- use the foo object > > end using > end sub > > > As said the above is my own opinion without warranty`s especially about > example B i get a lot of nasty responses however it is a Balena example > written in the core reference guide of VB.net,, so i guess it should be a > valid situation . > > I know this thread is going to explode ( as it normally does on this > subject ) with all people telling you something different , i would say > form your own opinion > by reading about the GC on MSDN then you will see what it does and what > it doesn`t , and especially this is verry important , setting a object to > Nothing does not mean that you will get your resources inmediatly back , > however in resource hungry systems it might make a difference in the right > scenario . > > regards > > Michel Posseth [MCP] > > > > > > > <param@community.nospam> schreef in bericht > news:eGGwBDV9GHA.1172@TK2MSFTNGP03.phx.gbl... >> Hi all, >> >> Coming from the good old VB6 days we were told to always destroy our >> objects as follows:- >> >> Dim obj as New MyObject >> ' do some work with obj >> obj = Nothing >> >> I have been doing this in .Net also for quite a while now. The other day >> one of my peers mentioned that this is a BIG NO NO and that I should let >> the Garbage Collection process handle this for me. >> >> Can someone share any insight on this? >> >> TIA! >> > > I did not set anny object private in my example code ,,,, i did discuss this
in my example C as a hypothese in this cas it might be valid if you use the same object in multiple methods ( so the object is declared in the hole class context ) if a certain codition is met then you might choose to destroy the object explicit in my opinion this is a valid reasson to set a object to Nothing regards Michel Show quoteHide quote "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schreef in bericht news:eMwP6el9GHA.3352@TK2MSFTNGP03.phx.gbl... > Michel, > > What can be the reason that you set in your first exsample 2 the object > privat. > > In both situations is their in my opinion (if you set them in your method > at the end to nothing) not any reason to do that, although it seems to be > a kind of obfuscating from some developers to set everything global. > > Cor > > > > Only the last one will stay if you don't set it to nothing. > "Michel Posseth [MCP]" <M***@posseth.com> schreef in bericht > news:OsjqzQc9GHA.4572@TK2MSFTNGP02.phx.gbl... >> Hello ,,, >> >> Well you have started something :-) ....... this has been discussed >> manny times in this group and it always ends the same :-) >> >> The below is my opinion , wich is formed by following a lot of these >> discussions and how it is described in the manny books i own >> >> The BIG NO NO is a bit odd , he would be right if he would have said >> that it isn`t necesary and that you are typing a line to much >> if your object is declared in method scope >> >> but there are situations thinkable where it is valid to set a object to >> Nothing ( however never absolut necesary , as it could have been in >> VB6 ) >> >> so here are some scenario`s for you to rethink : >> >> Example A >> Private sub Useobject () >> dim foo as new Objfoo.lib >> >> ---- use the foo object >> >> --- setting the foo object here to nothing is absolute not necesary >> --- as soon as it runs out of scope it is marked for collection >> end sub >> >> Example B >> >> Private sub Useobject () >> dim foo as new Objfoo.lib >> >> for i as integer = 0 to 1000 >> >> if i < 500 then >> ---- use the foo object >> else >> ---- stop using the foo object >> end if >> next >> end sub >> >> --- here is a situation where it is valid to set the foo object to >> nothing in the else statement ( if i = 500 then foo=Nothing ) >> --- especially when it is a resource hungry object , However it is not >> guaranteed that the object is collected at this point >> end sub >> >> Example C >> The object is declared in class scope ( because you use it withevents or >> whatever reasson you might have ) >> and for some reasson you can live without it , for a time and if needed >> you can declare a new instance >> i encountered this scenario myself in a remoting scenario wich used a >> singleton >> >> >> if you use VB.Net 2005 then learn yourself to use the using stetement ( >> it makes sure everything is nicely cleaned up , and as a bonus it makes >> your code nicer readable ) >> >> example >> >> Private sub Useobject () >> using foo as new Objfoo.lib >> >> ---- use the foo object >> >> end using >> end sub >> >> >> As said the above is my own opinion without warranty`s especially about >> example B i get a lot of nasty responses however it is a Balena example >> written in the core reference guide of VB.net,, so i guess it should be a >> valid situation . >> >> I know this thread is going to explode ( as it normally does on this >> subject ) with all people telling you something different , i would say >> form your own opinion >> by reading about the GC on MSDN then you will see what it does and what >> it doesn`t , and especially this is verry important , setting a object to >> Nothing does not mean that you will get your resources inmediatly back , >> however in resource hungry systems it might make a difference in the >> right scenario . >> >> regards >> >> Michel Posseth [MCP] >> >> >> >> >> >> >> <param@community.nospam> schreef in bericht >> news:eGGwBDV9GHA.1172@TK2MSFTNGP03.phx.gbl... >>> Hi all, >>> >>> Coming from the good old VB6 days we were told to always destroy our >>> objects as follows:- >>> >>> Dim obj as New MyObject >>> ' do some work with obj >>> obj = Nothing >>> >>> I have been doing this in .Net also for quite a while now. The other day >>> one of my peers mentioned that this is a BIG NO NO and that I should let >>> the Garbage Collection process handle this for me. >>> >>> Can someone share any insight on this? >>> >>> TIA! >>> >> >> > >
Backing up with SQL MO
Best way to store dataset to disk??? Query results to file Transparent BackGround Newb looking for data binding help Datasets and Adapter Updates Webbrowser control - buffering - audio/video playback problem... Using VB and ADO.NET validate radio button in groupbox What is the event to trap when user clicks X to close form? |
|||||||||||||||||||||||