Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Does setting object reference to null help the GC in collection

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Shree - 26 Mar 2008 09:54 GMT
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
Marc Gravell - 26 Mar 2008 10:02 GMT
It depends on the context ;-p

If you mean a method variable in a short duration method, then no, not
really. In fact, a check like "if (foo!=null) {foo=null;}" could /
extend/ the theoretical lifetime by making it last at least until the
check (rather than the last genuine time the variable is read).

However: If you mean a field on a class that is going to stay alive,
then: definitely. But if that class instance is going out of scope
anyway, no again...

Clear as mud?

Marc
Peter Morris - 26 Mar 2008 10:07 GMT
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
Brian Gideon - 26 Mar 2008 14:41 GMT
On Mar 26, 4:07 am, "Peter Morris"
<peter[dot]morris(at)capableobjects.com> wrote:
> I believe not, take this example
>
[quoted text clipped - 26 lines]
>
> 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/archive/2003/04/19/51365.aspx

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.