17

I have a ConcurrentDictionary in my session class.
Key is an interface representing a manager class.
Value is a List of DataContracts classes that are used for that manager in this session.

When I dispose the session class, I wish to clear this dictionary. I need to clear all values and keys, but i cant dispose the keys - since they still exists after class dispose..

Is this enough ? - will this cause the GC to do the work ?

_myDictionary = null;

Or i need to iterate with a foreach on all keys and use the Remove to clear values.

ilansch
  • 4,784
  • 7
  • 47
  • 96
  • That will suffice. However, if anything other than the dictionary retains references to the values, then of course the values will not be freed until those other references go away. – Matthew Watson Jun 24 '13 at 08:58

1 Answers1

26

When I dispose the session class, I wish to clear this dictionary.

Why? If the session instance is going to become eligible for garbage collection, and the session is the only object which refers to the dictionary, the dictionary will become eligible for garbage collection.

Is this enough ? - will this cause the GC to do the work ?

It's almost certainly unnecessary. If anything else has a reference to the dictionary, then setting that variable to null will have no effect. If nothing else has a reference to the dictionary and the session is going to become eligible for garbage collection, then you don't need to do this at all.

The only time it's worth setting a variable to null for the sake of garbage collection is when that variable itself will live on (e.g. it's an instance variable in an object which is not going to be garbage collected, or it's a static variable).

Note that garbage collection is entirely separate to "disposing" of objects, by the way. Dispose and IDisposable are generally concerned with unmanaged resources, and there's no indication in your question that that concept is relevant here.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • since dictionary is managed resource, when disposing the session class, it will be GC automatically ? No other class hold reference to this dictionary, but the Key of a certain key/value set is a reference to a class that will live after session is exposed, so I am note sure maybe this can interfer the dispose. And this class implements a IDispsoable best practice pattern – ilansch Jun 24 '13 at 09:13
  • 1
    @ilansch: Again, disposal and garbage collection are separate. If nothing else refers to the instance of the session, then the session can be garbage collected. The fact that a key isn't garbage collected doesn't stop the dictionary from being garbage collected - the relationship isn't from the key to the dictionary, but the other way round. – Jon Skeet Jun 24 '13 at 09:15
  • 1
    Ok. thanks for response, My confusion was since i also hold a callback delegate in this session as a private member, this delegate must be set to null when session disposed. otherwise a reference still exists from session to a long-living class. – ilansch Jun 24 '13 at 09:18