When I'm working with a set of non-struct object-object dictionaries (Dictionary<ClassA,ClassB>
) will I do the GC a favor when I call Clear()
on all of them?
Or more interresting: Could it do any harm to the GC when I do this?
All objects including the dictionaries are expected to be in generation 2 since it's a long running transformation which will also generate a large amount of short and medium lived helper objects.
Update for the comments: The objects itself are mainly loaded by an EntityFramework context (so the dictionary is not the GC root) and the dictionaries are not resused since used in a context class like so:
internal sealed class TransformationContext : IDisposable
{
private readonly Dictionary<ClassA,ClassB> dict1 = new ...;
private readonly Dictionary<ClassC,ClassD> dict2 = new ...;
private readonly Dictionary<ClassE,ClassF> dict3 = new ...;
public void Dispose() // class is sealed, classic Dispose-pattern not needed
{
dict1.Clear();
dict2.Clear();
dict3.Clear();
}
}
using(var context = new TransformationContext(some, input, values))
[...]