473,396 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Set = Nothing

Seems that setting an object = Nothing no longer does anything in .NET.
Garbage collection destroys the object eventually. So, does it ever make
sense to set an object = Nothing in .NET?

Thanks.
Nov 20 '05 #1
4 6510
rrr
I think you're right.. also I have the same feeling.. hope
someone will answer this
Nov 20 '05 #2
If you want to do your garbage collection early, yeah...

Sometimes... Objects get big... =)

"David Schwartz" <da**@hr-symphony.com> wrote in message
news:uz**************@TK2MSFTNGP11.phx.gbl...
Seems that setting an object = Nothing no longer does anything in .NET.
Garbage collection destroys the object eventually. So, does it ever make
sense to set an object = Nothing in .NET?

Thanks.

Nov 20 '05 #3
Hi David,

If the variable is going out of scope, eg it's a local variable or within
a for loop, etc, then there is no advantage in setting it to Nothing. In fact
it may be disadvantageous.

David Notario from the Microsoft JIT compiler team was discussing this
issue. He was adamant that setting variables to Nothing was generally a <bad>
idea within local scope as it upset the compiler's ability to optimise garbage
collection. In the normal case the compiler knows where you last <use> a
variable. At that point it can make the variable available for garbage
collection. If you have an assignment of Nothing then this moves the last-use
position further down in the code and this could mean that the variable hangs
around. But then it's common to set to Nothing just after calling Close,
Dispose or Quit, so that wouldn't make any difference. But David also said
that if the last thing you do with a variable is set it to Nothing, the
compiler will optimise the assignment away.

In this example:
Sub Foo (oSomethingVeryLarge As SomeType)
DoSomethingWith (oSomethingVeryLarge)
oSomethingVeryLarge = Nothing 'Irrelevant!!
DoSomethingLongRunning
End Sub

It seems sensible to set oSomethingVeryLarge to Nothing so that the GC can
collect the memory while SomethingLongRunning is taking place. In fact the
compiler removes the assignment.

But that means, you cry in alarm, that the reference to the memory is
still there and the garbage collector won't collect it. Not so, says David,
reassuringly. The compiler knows that oSomethingVeryLarge is not used after
DoSomethingWith() and makes it available for collection <at that point>
automatically - regardless of the assignment to Nothing.

That's local scope. - a lot of words to say don't bother with Nothing. :-)

If it's a long-term scope variable, however, (eg in a Module or an object
that has a long lifetime), then once that particular variable is no longer of
use, set it to Nothing at the earliest opportunity.

The most important thing to remember is that you use Close or Dispose
whenever an object has them. If you fail to do your closing and disposing then
the resources used by your object will be retained until the garbage collector
calls the object's Finalize. This may take some time. And it's not guaranteed.

Regards,
Fergus
==============================
<quote> In general, I would STRONGLY recommend against nulling out locals
</quote>
David Notario,
Software Design Engineer - CLR JIT Compiler

The full discussion in [microsoft.public.dotnet.framework.performance] about
whether to set variables to Nothing.
via: http://tinyurl.com/ouof
Nov 20 '05 #4
David,
In addition to the info Fergus stated.

I normally only set an object = nothing when I want to ensure that I no
longer use that instance of the object, but I am not ready to have a new
instance in its place (in a loop for example).

However I am careful when I do this, as sometimes using the NullObject
pattern is better. The NullObject pattern is a specific case of the Special
Case Pattern.

http://www.martinfowler.com/eaaCatalog/specialCase.html

Instead of checking for an object = nothing, I will create a derived object
from the primary class that simply returns 'defaults' then I use this object
in place of setting the variable to nothing...

Normally my NullObjects follow the Singleton Pattern.

Hope this helps
Jay

"David Schwartz" <da**@hr-symphony.com> wrote in message
news:uz**************@TK2MSFTNGP11.phx.gbl...
Seems that setting an object = Nothing no longer does anything in .NET.
Garbage collection destroys the object eventually. So, does it ever make
sense to set an object = Nothing in .NET?

Thanks.

Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

96
by: BadPony | last post by:
Anyone using Peoplesoft on a Federated UDB (shared nothing)Environment on Open System Platforms? Preferably AIX, but any war stories would be good. TEA EB-C
24
by: Hardy | last post by:
I'm pretty new in this field. when reading some 70x material, I met with this term but cannot catch its accurate meaning. who can help me? thanks in advance:)~
6
by: Bob Day | last post by:
VS 2003 The documentation says " Nothing keyword represents the default value of any data type" this is simply not true and causing a lot of problems. 1) Consider an SQL table of 3 columns: ...
26
by: Bob Day | last post by:
VS 2003, vb.net, sql native (MSDE)... I have railed against the inconsistency of the Nothing key word. The documentation says is will assign a default value for any datatype...well, not...
5
by: John A Grandy | last post by:
do these mean the same thing ? Dim s As String ..... If s Is Nothing Then ..... If s = Nothing Then .....
8
by: Tiraman | last post by:
Hi , i would like to get some explanation about using the Obj = Nothing . For example , I have a function that hold a local object like in this example public function Test() dim objMyCol...
12
by: Mike Eaton | last post by:
Hi all, What do people regard as the best practice with respect to freeing object references when you're done with them? Some people I've worked with in the past suggested that if you create an...
9
by: Doug Glancy | last post by:
I got the following code from Francesco Balena's site, for disposing of Com objects: Sub SetNothing(Of T)(ByRef obj As T) ' Dispose of the object if possible If obj IsNot Nothing AndAlso...
6
by: DippyDog | last post by:
This is an old old post that I'm referencing regarding what happens when you set an integer variable to Nothing. It gets set to zero, not "Nothing." ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.