public void ScavengeOnGrow() { var dictionary = new WeakDictionary <object, object>(); for (int i = 0; i < 100; i++) { dictionary[new Object()] = new Object(); // Randomly collect some if (i == 15) { GC.Collect(); } } // We should have scavenged at least once Console.WriteLine("Count {0}", dictionary.Count); Assert.Equal(true, dictionary.Count < 100); // Finish with explicit scavenge int count1 = dictionary.Count; int removed = dictionary.Scavenge(); int count2 = dictionary.Count; Console.WriteLine("Removed {0}", removed); Assert.Equal(removed, count1 - count2); }
public void ExplicitScavenge() { object k1 = new Object(); object v1 = new Object(); var dictionary = new WeakDictionary <object, object>(); dictionary[k1] = v1; Assert.Equal(1, dictionary.Count); k1 = null; GC.Collect(); dictionary.Scavenge(); Assert.Equal(0, dictionary.Count); }
public void ScavengeOnGrow() { var dictionary = new WeakDictionary<object, object>(); for (int i = 0; i < 100; i++) { dictionary[new Object()] = new Object(); // Randomly collect some if (i == 15) { GC.Collect(); } } // We should have scavenged at least once Console.WriteLine("Count {0}", dictionary.Count); Assert.Equal(true, dictionary.Count < 100); // Finish with explicit scavenge int count1 = dictionary.Count; int removed = dictionary.Scavenge(); int count2 = dictionary.Count; Console.WriteLine("Removed {0}", removed); Assert.Equal(removed, count1 - count2); }
public void ExplicitScavenge() { object k1 = new Object(); object v1 = new Object(); var dictionary = new WeakDictionary<object, object>(); dictionary[k1] = v1; Assert.Equal(1, dictionary.Count); k1 = null; GC.Collect(); dictionary.Scavenge(); Assert.Equal(0, dictionary.Count); }