Should I assign null to my local variables?

Published 26 March 04 08:15 PM

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]

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Paul Wilson said on March 27, 2004 4:37 AM:
.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.
# Jon Skeet said on March 27, 2004 10:38 AM:
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).
# Paul Wilson said on March 27, 2004 12:30 PM:
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.
# deepblue said on April 13, 2004 11:01 AM:
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 cents
# Miguel de Icaza said on April 19, 2004 2:07 PM:
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.
# Jon Skeet said on April 20, 2004 2:16 AM:
Urgh - could I suggest changing that then? It's rather nasty if people ought to start telling the compiler when they've finished with a variable when the information is fairly easily available from the compiled IL.
# JJBlog said on May 11, 2004 4:33 PM:
# andy said on May 17, 2004 10:27 PM:
<a href=http://www.iwo100.com>?????</a>
# ok said on July 18, 2004 12:30 AM:
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.htm
# Eric Joe said on September 16, 2004 2:02 AM:
Ping Back来自:blog.csdn.net
# RebelGeekz said on December 28, 2004 4:54 AM:
[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/]
# { public virtual blog; } said on January 5, 2005 10:36 AM:
# Objektvernichtung und GarbageCollection | hilpers said on January 20, 2009 9:18 AM:

PingBack from http://www.hilpers.com/309820-objektvernichtung-und-garbagecollection

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

This Blog

Syndication

Page view tracker