Before:
private static Dictionary<RuleKey, bool> mergeRules( Dictionary<RuleKey, bool> topPriorityRules, Dictionary<RuleKey, bool> secondaryRules) { var resolvedRules = topPriorityRules.Concat(secondaryRules.Where(kvp => !topPriorityRules.ContainsKey(kvp.Key))) .ToDictionary(x => x.Key, x => x.Value); return resolvedRules; } After:
private static Dictionary<TKey, TValue> merge<TKey, TValue>( Dictionary<TKey, TValue> topPriorityRules, Dictionary<TKey, TValue> secondaryRules) { var resolvedRules = topPriorityRules; foreach (var rule in secondaryRules) if(!resolvedRules.ContainsKey(rule.Key)) resolvedRules.Add(rule.Key, rule.Value); return resolvedRules; } There are 2 dictionaries of rights that need to be merged into one. In the first case, when calling the method 500 times. The amount of RAM occupied by the program reaches up to 1GB. In the second case, about 300MB. Why such differences in memory consumption? Indeed, in essence, the methods perform the same thing.