473,657 Members | 2,634 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.ed u
Nov 16 '05 #1
2 1771
Adam Clauss <ca*****@tamu.e du> 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.co m>
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.ed u
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message news:MP******** *************** *@msnews.micros oft.com...
Adam Clauss <ca*****@tamu.e du> 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.co m>
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
18057
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 possible to set the database treat the null values as zero? Thanks
7
1925
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. This object reference is passed into another object create within the same
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 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!
4
12833
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, which becomes invalid when the user installs the database (ie: c:\devpath\ becomes c:\program files\mydbapp, but the .Picture property still points to c:\devpath\) The problem is that I don't *know* what installation path the user will select,...
0
8407
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8319
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8837
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8512
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8612
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7347
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6175
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2739
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.