简体   繁体   中英

What is the importance of clearing the collection before disposing

What is the difference between the following codes

Code 1 :

if(RecordCollections!=null){
   RecordCollections.Clear();
   RecordCollections=null;
}

Code 2 :

RecordCollections = null;

The code is present inside a Dispose Method , Is there any advantage over using the Clear method before making the Collection to null ? Whether it is needed at all?

Is there any advantage over using the Clear method before making the Collection to null.

Impossible to say without a good Minimal, Complete, and Verifiable code example .

That said, neither of those code snippets look very useful to me. The first one for sure would be pointless if all that the Clear() method does is to empty the collection. Of course, if it actually went through and eg called Dispose() on each collection member, that would be different. But that would be a very unusual collection implementation.

Even the second has very little value, and is contrary to normal IDisposable semantics. IDisposable is supposed to be just for managing unmanaged resources. It gets used sometimes for other things, but that's its main purpose. In particular, one typically only calls Dispose() just before discarding an object. If the object itself is going to be discarded, then any references it holds to other objects (such as a collection) will no longer be reachable, and so setting them to null doesn't have any useful effect.

In fact, in some cases setting a variable to null can actually extend the lifetime of an object. The runtime is sophisticated enough to recognize that a variable is no longer used, and if it holds the last remaining reference to an object, the object can become eligible for garbage collection at that point, even if the scope of the variable extends further. By setting the variable to null , the variable itself is used later in the program, and so the runtime can't treat it as unreachable until that point, later than it otherwise would have.

This last point typically applies most commonly to local variables, not fields in an object. But it's theoretically possible for the runtime to optimize more broadly. It's a bad habit to get into, to go around setting to null variables that themselves aren't going to be around much longer.

Dispose refers to a mechanism to explicitly clean up the un-managed memory , since that cannot be cleaned up using standard Garbage Collector , mostly IDisposable will be implemented by the class, which use the unmanaged API internally like Database Connection .

Standard practice is:

  • To also implement Finalizer along with, since Dispose is an explicit call and if missed out by caller then Finalization does take care of clean up action, though it needs 2 cycles of GC.

  • If a class use any of the object, as a class variable, which implements a Dispose itself, then the Dispose shall be implemented to call the Dispose on the class variable.

Regarding the code provided:

if(RecordCollections!=null){
   RecordCollections.Clear();
   RecordCollections=null;
}

Or

RecordCollections = null;

As this is related to cleaning up managed memory, it has little use, since GC does the main job and doesn't need it, but in my view its an acceptable practice , where class variables are explicitly nullified, which makes user vary of each and every allocation and mostly an attempt shall be made to use the method local variables, until and unless state needs to be shared across method calls. Object allocation misuse, can be much more controlled.

As far as difference is concerned, though a collection is explicitly cleared and then nullified or just nullified, the memory remains intact, till the point GC is invoked, which is un-deterministic , but in my view its not very clear, how does the GC explicitly maps the objects for collection, which are no more reachable, but for various generations, especially the higher ones (promoted objects), if an object is explicit marked as null , then GC may have to spend less / no time tracing the root / reference, however there's no explicit documentation, to explain this aspect / implementation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM