Should you set all the objects to null
(Nothing
in VB.NET) once you have finished with them?
I understand that in .NET it is essential to dispose of any instances of objects that implement the IDisposable
interface to release some resources although the object can still be something after it is disposed (hence the isDisposed
property in forms), so I assume it can still reside in memory or at least in part?
I also know that when an object goes out of scope it is then marked for collection ready for the next pass of the garbage collector (although this may take time).
So with this in mind will setting it to null
speed up the system releasing the memory as it does not have to work out that it is no longer in scope and are they any bad side effects?
MSDN articles never do this in examples and currently I do this as I cannot see the harm. However I have come across a mixture of opinions so any comments are useful.
Some object suppose the
.dispose()
method which forces the resource to be removed from memory.Chances are that your code is not structured tightly enough if you feel the need to
null
variables.There are a number of ways to limit the scope of a variable:
As mentioned by Steve Tranby
Similarly, you can simply use curly brackets:
I find that using curly brackets without any "heading" to really clean out the code and help make it more understandable.
There are some cases where it makes sense to null references. For instance, when you're writing a collection--like a priority queue--and by your contract, you shouldn't be keeping those objects alive for the client after the client has removed them from the queue.
But this sort of thing only matters in long lived collections. If the queue's not going to survive the end of the function it was created in, then it matters a whole lot less.
On a whole, you really shouldn't bother. Let the compiler and GC do their jobs so you can do yours.
Karl is absolutely correct, there is no need to set objects to null after use. If an object implements
IDisposable
, just make sure you callIDisposable.Dispose()
when you're done with that object (wrapped in atry
..finally
, or, ausing()
block). But even if you don't remember to callDispose()
, the finaliser method on the object should be callingDispose()
for you.I thought this was a good treatment:
and this
There isn't any point in trying to second guess the GC and its management strategies because it's self tuning and opaque. There was a good discussion about the inner workings with Jeffrey Richter on Dot Net Rocks here: Jeffrey Richter on the Windows Memory Model and Richters book CLR via C# chapter 20 has a great treatment:
Another reason to avoid setting objects to null when you are done with them is that it can actually keep them alive for longer.
e.g.
will allow the object referred by someType to be GC'd after the call to "DoSomething" but
may sometimes keep the object alive until the end of the method. The JIT will usually optimized away the assignment to null, so both bits of code end up being the same.
Also: