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

Reference variable x GC

Hi,

I have seen some developers commenting that a reference variable is 'more
garbage collection eligible' if the programmer set its value to null after
using it, like this:

public void MemberFunction()
{
AnyClass refVar = new AnyClass ();

// Normal use of refVar functionalities

refVar = null;
}

Is it real? Any comment about?

Thanks,

Baccarin.
Nov 15 '05 #1
7 1310
In this case, you won't get any benefit unless a collection occurs after the
variable is set to null and before the stack collapses after the method
returns to the caller. Since refVar is about to be evicted from the stack,
there's really no benefit to setting it to null. In other cases where the
time delta is greater, a live local reference to an object on the heap will
cause the object to be tracable, and therefore not eligible for collection.
However, in most cases this is a micro optimization, and won't buy you a
lot - especially if you're talking about local variables in methods that are
reasonably-sized.

Note also that setting a reference to null is not the same thing as
Dispose'ing it.
--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com
"Pekus Cons. e Desenvolvimento" <pekus@REMOVE_pekus.com.br> wrote in message
news:ez****************@TK2MSFTNGP12.phx.gbl...
Hi,

I have seen some developers commenting that a reference variable is 'more
garbage collection eligible' if the programmer set its value to null after
using it, like this:

public void MemberFunction()
{
AnyClass refVar = new AnyClass ();

// Normal use of refVar functionalities

refVar = null;
}

Is it real? Any comment about?

Thanks,

Baccarin.

Nov 15 '05 #2
"Pekus Cons. e Desenvolvimento" <pekus@REMOVE_pekus.com.br> wrote:
I have seen some developers commenting that
a reference variable is 'more garbage collection
eligible' if the programmer set its value to null
after using it, [...]
Is it real? Any comment about?


References that are still in use can be null for many good reasons, so I'd
hope this isn't the case.

In fact, I suspect that setting the value of a variable at any point says to
the GC, "I am still changing this variable, so I obviously haven't finished
with it."

This is just speculation, though.

P.
Nov 15 '05 #3
Pekus Cons. e Desenvolvimento wrote:
Hi,

I have seen some developers commenting that a reference variable is 'more
garbage collection eligible' if the programmer set its value to null after
using it, like this:

public void MemberFunction()
{
AnyClass refVar = new AnyClass ();

// Normal use of refVar functionalities

refVar = null;
}


For local variables, the JIT compiler determines where the last use of
that variable is in the method. The JIT marks the reference as dead
after that point, so the GC can collect whatever is referenced by that
local (as long as no other live reference to the object exists).

In other words, in general, setting a reference to null does not help
anything when the reference is maintained in a local variable.
--
mikeb
Nov 15 '05 #4
Paul E Collins <fi******************@CL4.org> wrote:
I have seen some developers commenting that
a reference variable is 'more garbage collection
eligible' if the programmer set its value to null
after using it, [...]
Is it real? Any comment about?


References that are still in use can be null for many good reasons, so I'd
hope this isn't the case.


I think there's really a mismatch of terminology here. Variables
themselves aren't garbage collected - objects are. Setting the value of
a variable to null can, in *some* situations, shorten the lifetime of
an object. It can never shorten the lifetime of an actual object.
Usually it's not worth doing though, as the JIT compiler can usually
see when a variable is no longer used even if it is still theoretically
"live".

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

"Pekus Cons. e Desenvolvimento" <pekus@REMOVE_pekus.com.br> wrote in message
news:ez**************@TK2MSFTNGP12.phx.gbl...
I have seen some developers commenting that a reference variable is 'more
garbage collection eligible' if the programmer set its value to null after
using it....


It's not so much a problem with local variables in a function. What you have
to watch out for is member variables in a class. If you don't set the member
variable to null if it's not needed any more, but the instance of the class
is still being referenced, the memory consumed by the member variable won't
be garbage-collected until the parent object is collectible. In that
situation you would want to be sure to set the member variable to null.
Nov 15 '05 #6
Bret Mulvey [msft] <br***@online.microsoft.com> wrote:

"Pekus Cons. e Desenvolvimento" <pekus@REMOVE_pekus.com.br> wrote in message
news:ez**************@TK2MSFTNGP12.phx.gbl...
I have seen some developers commenting that a reference variable is 'more
garbage collection eligible' if the programmer set its value to null after
using it....


It's not so much a problem with local variables in a function. What you have
to watch out for is member variables in a class. If you don't set the member
variable to null if it's not needed any more, but the instance of the class
is still being referenced, the memory consumed by the member variable won't
be garbage-collected until the parent object is collectible. In that
situation you would want to be sure to set the member variable to null.


Do you run into situations frequently? I very rarely end up in the
situation where I know that I don't need *part* of the state of an
object any more, but that the rest of the object is still required.
When I come across situations like that I usually find that a redesign
of the class gets rid of the problem and ends up with a cleaner
solution anywa6y.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7
I've been away from this ng for a while ~ good to
see you got your MVP, Jon.

--
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Nov 15 '05 #8

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

Similar topics

2
by: Kench | last post by:
I was curious and playing with pointers and references to see what's different between them. Other than the obvious ones involving C++ syntax & things like references cannot be modified with...
2
by: Jim Red | last post by:
hello first of all, i know, there are no classes in javascript. but i will use that word for better understanding of my question. here we go. i have three classes and need a reference to the...
26
by: Dave Hammond | last post by:
In document "A.html" I have defined a function and within the document body have included an IFRAME element who's source is document "B.html". In document "B.html" I am trying to call the function...
19
by: daniel | last post by:
This is a pretty basic-level question, but I'd really like to know, so thanks for any help or pointers you can provide (like what I would google for ;o) Suppose: <code> myFunc() {
2
by: Jake Barnes | last post by:
Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is...
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
10
by: ravi | last post by:
Hi, i am a c++ programmer, now i want to learn programming in c also. so can anybody explain me the difference b/w call by reference and call by pointer (with example if possible).
4
by: =?Utf-8?B?QWxla3MgS2xleW4=?= | last post by:
When I call sub in VB I can sent value of variable or reference to this value. The question is. Suppose I have class public class AA .... end class and declare object of this class dim objAA as...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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...

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.