<p>Is it necessary to set large objects to <code>null</code> when implementing a <code>Dispose()</code> method?</p>

[{'answer_id': 49957, 'author': 'Espo', 'author_id': 2257, 'author_profile': 'https://Stackoverflow.com/users/2257', 'pm_score': 3, 'selected': False, 'text': '<p>If a class has a Dispose method the best practice is to call it. the reason\nbehind that is that Dispose runs when called, whereas setting the object to\nnull simply adds a entry to the Finalize queue in GC, and we cannot\ndetermine when GC will run.</p>\n\n<p>There is no performance benefit in implementing the Dispose method on types that use only managed resources (such as arrays) because they are automatically reclaimed by the garbage collector. Use the Dispose method primarily on managed objects that use native resources and on COM objects that are exposed to the .NET Framework. Managed objects that use native resources (such as the FileStream class) implement the IDisposable interface.</p>\n\n<p>An elegant means of inoking Dispose that have adopted is using the "using" construct. For those of you who may not be familiar with the construct, it provide a means to implicity invoke Dispose() on an instance that implements IDisposable even if an exception is thrown durring the operation. The following is an example of the using construct: </p>\n\n<pre><code>using(DisposableClass dc = new DisposableClass()) \n{ \n   dc.PerformActionOnUmanagedResources(); \n   dc.PerformAnotherActionOnUmanagedResources(); \n} \n</code></pre>\n\n<p>In the previous example, if an exception was thrown in the PerformActionOnUmanagedResources() method, although the PerformAnotherActionOnUmanagedResources() method would not be processed, the using block will still implicity invoke the Dispose method on dc ensuring the realese of any unmanaged resources.</p>\n'}, {'answer_id': 49958, 'author': 'Mark Ingram', 'author_id': 986, 'author_profile': 'https://Stackoverflow.com/users/986', 'pm_score': 2, 'selected': False, 'text': '<p>The purpose of a dispose method is to release all resources associated with your class, and the parent\'s class by calling the base class dispose method. Have a read of this link, it should make things a little clearer:</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx</a></p>\n'}, {'answer_id': 49959, 'author': 'pilif', 'author_id': 5083, 'author_profile': 'https://Stackoverflow.com/users/5083', 'pm_score': 1, 'selected': False, 'text': '<p>what do you mean by "large object"?</p>\n\n<p>You should at least call Dispose() on any member implementing IDisposable, though.</p>\n'}, {'answer_id': 49961, 'author': 'Brad Wilson', 'author_id': 1554, 'author_profile': 'https://Stackoverflow.com/users/1554', 'pm_score': 4, 'selected': True, 'text': "<p>Not usually.</p>\n\n<p>The garbage collector looks for rooted objects, and circular dependencies don't prevent collection if neither object is rooted.</p>\n\n<p>There is a caveat: if object A has a reference to object B, and object B is being disposed, you may want to clean up that relationship or else you could end up with a leak. The most common place this surfaces is in event handlers (the reference from A->B is one that B controls, because it subscribed to an event on A). In this case, if A is still rooted, B cannot be collected even though it's been disposed.</p>\n"}, {'answer_id': 50012, 'author': 'Rob Walker', 'author_id': 3631, 'author_profile': 'https://Stackoverflow.com/users/3631', 'pm_score': 1, 'selected': False, 'text': "<p>It isn't necessary as others have pointed out, but it is good practice and helps with debugging.</p>\n\n<p>Once an object has finished with a pointer it is using then setting it to null helps prevent reuse of that object later (you'll get an null reference exception).</p>\n\n<p>The same logic applies to setting member pointers to null in C++ destructors once you have deleted them.  There is no <em>need</em> to do it, but it helps with troubleshooting later.</p>\n"}, {'answer_id': 917247, 'author': 'Jason Baker', 'author_id': 2147, 'author_profile': 'https://Stackoverflow.com/users/2147', 'pm_score': 0, 'selected': False, 'text': '<p>Think about the purpose of Disposable methods for a bit:  it\'s usually because you\'re holding some resource that won\'t be released during garbage collection.  This is usually something like a database connection or a file handle.  Thus, once the Dispose method has been called, all those resources have been released.</p>\n\n<p>I\'d argue that having nulls floating around is more harmful that having "zombie" objects floating around.</p>\n'}]
['https://Stackoverflow.com/questions/49950', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4770/']