473,387 Members | 1,501 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,387 software developers and data experts.

Does setting object reference to null help the GC in collection

I am having this confusion going on in my head for sometime now. The
question is whether setting the object reference to null (nothing)
help the GC in collection in anyway?
Can anyone shed some light on this? Is there any way to validate this
proposition? Or is it just a myth...

-Thanks,
Shree
Mar 26 '08 #1
2 1965
I believe not, take this example

object x = new Person();
x.FirstName = "Peter";
x.LastName = "Morris";

//Position A

...lots of code here..

//Position B
x.FullName = x.FirstName + " " + x.LastName;

//Position C

...lots of code here..
The first thing to remember is that the GC can interrupt this method at any
point. If it interrupts at position A it will not collect the value in "x"
because the value is referenced later in the method. If it interupts at
position C it will be able to collect the value in "x" because it is no
longer used.

In summary

The object is only held onto in this example if the variable is referenced.
The fact that the object is referenced by a variable is irrelevant if that
variable is not itself referenced.

Pete
Mar 26 '08 #2
On Mar 26, 4:07*am, "Peter Morris"
<peter[dot]morris(at)capableobjects.comwrote:
I believe not, take this example

object x = new Person();
x.FirstName = "Peter";
x.LastName = "Morris";

//Position A

..lots of code here..

//Position B
x.FullName = x.FirstName + " " + x.LastName;

//Position C

..lots of code here..

The first thing to remember is that the GC can interrupt this method at any
point. *If it interrupts at position A it will not collect the value in "x"
because the value is referenced later in the method. *If it interupts at
position C it will be able to collect the value in "x" because it is no
longer used.

In summary

The object is only held onto in this example if the variable is referenced..
The fact that the object is referenced by a variable is irrelevant if that
variable is not itself referenced.

Pete
Strangley, it can be collected in position B halfway through the call
to FullName as long as it's after any explicit or implicit use of the
'this' pointer inside that property.

Here is a link that explains why this might be important to you and
gives a example of where you would use GC.KeepAlive.

http://blogs.msdn.com/cbrumme/archiv.../19/51365.aspx
Mar 26 '08 #3

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

Similar topics

20
by: | last post by:
If I need to check if a certain value does exist in a field, and return either "yes" or "not" which query would be the most effestive?
8
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
2
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...
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: ...
0
by: Richard Gregory | last post by:
Hi, I have the wsdl below, for an Axis web service, and when I select Add Web Refernce in Visual Studio the proxy is missing a class representing the returnedElementsType (see reference.cs below...
51
by: Tony Sinclair | last post by:
I'm just learning C#. I'm writing a program (using Visual C# 2005 on WinXP) to combine several files into one (HKSplit is a popular freeware program that does this, but it requires all input and...
48
by: avasilev | last post by:
Hi all, my question is: if i allocate some memory with malloc() and later free it (using free()), is there a possibility that a consequent malloc() will allocate memort at the same starting...
19
by: Angus | last post by:
I have a socket class CTestClientSocket which I am using to simulate load testing. I create multiple instances of the client like this: for (int i = 0; i < 5; i++) { CTestClientSocket* pTemp...
0
by: dprjessie | last post by:
Hello, I am a Web programmer and I'm working on my first desktop application as a favor for a friend. I'm sure I have a stupid error here, but there is no error being thrown so I can't figure out...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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...

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.