473,401 Members | 2,068 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,401 software developers and data experts.

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 1758
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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.