473,763 Members | 1,333 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1979
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)capab leobjects.comwr ote:
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
10161
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
2559
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 in global space, therefore it is not a global variable, yes? Even if it was global, how would it get from one function to another? In PHP variables are copied by value. Are they copied by reference in Javascript? <SCRIPT LANGUAGE="JavaScript">
2
1779
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!
6
1704
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: Column1 bit no nulls Column2 string no nulls Column3 DateTime no nulls
0
5115
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 the wsdl). This complex type is a collection of another complex type(elementType), and the Reference.cs has an array of these rather than the single returnedElementsType. If If I want to be able to obtain these elements from the SOAP response I...
51
3957
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 output to be within one directory, and I want to be able to combine files from different directories into another directory of my choice). My program seems to work fine, but I'm wondering about this loop: for (int i = 0; i < numFiles; i++)
48
5843
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 address and will return the same pointer as the previous malloc(). I would like to have confirmation on whether this is practically a concern when pointers are used to uniquely identify data structure instances - like in this example:
19
4199
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 = new CTestClientSocket(this, ip, port); pTemp->Connect(); m_collClients.push_back(pTemp);
0
1752
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 what is wrong. I have code that reads an excel file and fills a datagridview and it works just fine. It is also supposed to fill a database though and that is not working. The executeNonQuery command is returning a 1 which as I understand, means it has...
0
9563
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
9386
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
9998
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
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
8822
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...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3917
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
3
3523
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.