473,241 Members | 1,987 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,241 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 1306
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.