will clearing dictionaries do the garbage collector a favor?

我与影子孤独终老i 提交于 2019-12-25 00:13:21

问题


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))
  [...]

来源:https://stackoverflow.com/questions/32819098/will-clearing-dictionaries-do-the-garbage-collector-a-favor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!