Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Q: Should I assign null to my local variables after I use them?
For example:
string s = ...;
Console.WriteLine(s);
s = null;
A: There is rarely a need to do this for local variables in C#
The lifetime of variables is tracked by the JIT - it analyzes how variables are used in a routine, and it knows exactly when the variable is no longer needed, and after that point, the value is available to be collected.
Interestingly, if you assign it to null, you are actually extending the lifetime of the variable slightly, so it could cause it to be garbage collected later (though realistically, there's unlikely to be any real difference here.
This is true for the vast majority of methods. If you have a method where your code lives for a while - a loop on a separate thread, for example - then it may be worthwhile to see if there are any unnecessary values living in variables while you wait.
[Author: Eric Gunnerson]
Comments
Anonymous
March 26, 2004
.NET Garbage Collection Myth #1:
There is never any reason to setting your objects to null (or nothing in VB) since the GC will do this for you automatically.
Fact:
There are times where setting your objects to null (or nothing in VB) can make huge differences in your memory footprint.
The details and downloadable code to reproduce this is on my blog:
http://weblogs.asp.net/pwilson/archive/2004/02/20/77422.aspx
Yes, this is only an issue in some rather odd cases, and I would agree a better design would not have had the same degree of issues, but the irrefutable point is that sometimes setting objects to null does make a difference, so lets please avoid the overly simplistic answers since some of us really do work in large enterprise systems.Anonymous
March 27, 2004
Eric didn't actually make any overly simplistic answers - hence the "rarely" and "vast majority" rather than "never" and "all".
As you say, a better design usually gets round this as a side effect - and I'd always argue that good design is especially important in "large enterprise systems". Keeping methods short and sweet has bigger implications than just this issue, but it usually solves this one as well.
Point of terminology: you can never set an object to null - you can only set a variable's value to null. The terminology of "setting an object to null" can be very confusing to newcomers (as well as being just plain wrong).Anonymous
March 27, 2004
By the way Jon, Eric updated his post after my comment -- thus the overly simplistic answer is not what you see now. I don't have a problem with that, as I am glad he updated it a little, but I did just want to point it out.Anonymous
April 13, 2004
If GC is becoming a bottleneck, you might want to consider implementing your own Dispose() call. This way, possibly, you could leave everything else to GC other than the code that you think needs to handle memory efficiently right away.
My 2 centsAnonymous
April 19, 2004
Well, that is implementation specific to .NET's VM and .NET's JIT engine.
In Mono, since we use a different kind
of garbage collector, setting the variable
to null would help the GC.
Miguel.Anonymous
April 19, 2004
The comment has been removedAnonymous
May 17, 2004
<a href=http://www.iwo100.com>?????</a>Anonymous
July 17, 2004
my web:
http://www.sj55.com/pic_sort http://www.zw88.com/paopaotang.htm http://www.zw88.com/sj.htm http://www.zw88.com/sm/
http://www.zw88.com/sms/ http://www.zw88.com/zw.htm http://www.resou.com/8888.htm http://www.sj55.com/pic/pic_1130.htmAnonymous
September 15, 2004
Ping Back来自:blog.csdn.netAnonymous
December 27, 2004
[http://itpeixun.51.net/][http://aissl.51.net/][http://kukuxz003.freewebpage.org/][http://kukuxz001.51.net/][http://kukuxz003.51.net/][http://kukuxz005.51.net/][http://kukuxz002.51.net/][http://kukuxz004.freewebpage.org/][http://kukuxz007.51.net/][http://kukuxz001.freewebpage.org/][http://kukuxz006.51.net/][http://kukuxz002.freewebpage.org/][http://kukuxz004.51.net/][http://kukuxz008.51.net/][http://kukuxz009.51.net/][http://kukuxz005.freewebpage.org/][http://kukuxz006.freewebpage.org/][http://kukuxz007.freewebpage.org/][http://kukuxz009.freewebpage.org/]Anonymous
January 20, 2009
PingBack from http://www.hilpers.com/309820-objektvernichtung-und-garbagecollectionAnonymous
January 01, 2011
ashigakari.com