开发者

In .NET, will cleaning a nested dictionaries parent release all memory upon garbage collection?

开发者 https://www.devze.com 2022-12-25 10:11 出处:网络
I have the following nested dictionaries: Dictionary<int, Dictionary<string, object>> x; Dictionary<int, SortedDictionary<long, Dictionary<string, object>>> y;

I have the following nested dictionaries:

  1. Dictionary<int, Dictionary<string, object>> x;
  2. Dictionary<int, SortedDictionary<long, Dictionary<string, object>>> y;

If I do x.Clear() and y.Clear() will a开发者_Python百科ll the nested objects clear and all the memory will be reused on the next garbage collection?

Or do I need to iterate on all the items and clear them manually?


If none of your objects are reachable from other parts of your code, they will all be garbage collected.

If this will be done on the next garbage collection, depends on the generation that they belong to.

This article from Jeffrey Richter explains a lot.


It's the same for everything: the GC starts from its roots (variables on the stack and various other odd cases) and walks the tree of reachable objects by following references. Any objects not found by that process will be eligible for collection.

So as long as your removed sub-dictionaries are not reachable some other way, they will be collected.


Maybe yes.

If you have references to items in the dictionary, regardless of the type of the value parameter, than those objects won't be collected.

But if there are no references, then they will be collected (at some point).

0

精彩评论

暂无评论...
验证码 换一张
取 消