473,513 Members | 2,406 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Difference between setting reference=null and going out of scope?

Basically, my question is in terms of performance and the garbage collector -
Is there any difference between
a) letting a variable simply go out of scope
b) explicity setting it to null once I am done with it

I have no particular reason to think that b) would gain me anything, but I thought maybe it would be a sure sign to the garbage
collector that Yes, you can go ahead and remove the object.

Thanks!

--
Adam Clauss
ca*****@tamu.edu
Nov 16 '05 #1
2 1765
Adam Clauss <ca*****@tamu.edu> wrote:
Basically, my question is in terms of performance and the garbage collector -
Is there any difference between
a) letting a variable simply go out of scope
b) explicity setting it to null once I am done with it
Yes - explicitly setting it to null might take slightly longer, if the
JIT doesn't notice that the write is useless.

Actually, when running under a debugger there'll be a difference in
that the object (assuming there are no other references to it) will be
eligible for garbage collection from the point at which the variable is
set to null, compared with having to actually wait for it to go out of
scope. In release mode, it doesn't even have to wait to go out of scope
or be set to null - after the last use of it, the GC won't treat it as
a live reference.
I have no particular reason to think that b) would gain me anything,
but I thought maybe it would be a sure sign to the garbage
collector that Yes, you can go ahead and remove the object.


Nope.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Interesting, alright, thanks for the info.

--
Adam Clauss
ca*****@tamu.edu
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message news:MP************************@msnews.microsoft.c om...
Adam Clauss <ca*****@tamu.edu> wrote:
Basically, my question is in terms of performance and the garbage collector -
Is there any difference between
a) letting a variable simply go out of scope
b) explicity setting it to null once I am done with it


Yes - explicitly setting it to null might take slightly longer, if the
JIT doesn't notice that the write is useless.

Actually, when running under a debugger there'll be a difference in
that the object (assuming there are no other references to it) will be
eligible for garbage collection from the point at which the variable is
set to null, compared with having to actually wait for it to go out of
scope. In release mode, it doesn't even have to wait to go out of scope
or be set to null - after the last use of it, the GC won't treat it as
a live reference.
I have no particular reason to think that b) would gain me anything,
but I thought maybe it would be a sure sign to the garbage
collector that Yes, you can go ahead and remove the object.


Nope.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3

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

Similar topics

2
18034
by: C. Lo | last post by:
Hi I have a situtation where a query returns a table in which some of the values are null. When I sort the results, the null values are treated as greater than the other numerical results. Is it...
7
1911
by: Nick Zdunic | last post by:
I have a remotable object running in my host application. The host starts up and creates the object. Within a method to start the remote object doing its thing it creates an object. ...
0
372
by: Adam Clauss | last post by:
Basically, my question is in terms of performance and the garbage collector - Is there any difference between a) letting a variable simply go out of scope b) explicity setting it to null once I am...
4
12816
by: NASAdude | last post by:
I'm working on distributing a database using VS Tools for Office, and am having trouble with linked images on forms/reports. The image objects have a default path\file set on the .Picture property,...
0
7166
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
7386
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7543
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...
1
7106
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7534
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
5689
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,...
1
5094
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3226
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
459
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.