Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 all 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?

like image 505
Tomer Pintel Avatar asked Sep 03 '25 05:09

Tomer Pintel


1 Answers

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.

like image 110
GvS Avatar answered Sep 04 '25 18:09

GvS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!