Exemplo n.º 1
0
        public void ScavengeOnGrow()
        {
            var dictionary = new WeakKeyDictionary <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
            this.logger.WriteLine("Count {0}", dictionary.Count);
            Assert.True(dictionary.Count < 100);

            // Finish with explicit scavenge
            int count1  = dictionary.Count;
            int removed = dictionary.Scavenge();
            int count2  = dictionary.Count;

            this.logger.WriteLine("Removed {0}", removed);
            Assert.Equal(removed, count1 - count2);
        }
        public void ExplicitScavenge()
        {
            var dictionary = new WeakKeyDictionary <object, object>();

            ExplicitScavenge_Helper(dictionary);

            GC.Collect();
            dictionary.Scavenge();

            Assert.Equal(0, dictionary.Count);
        }
Exemplo n.º 3
0
        public void ExplicitScavenge()
        {
            object k1 = new object();
            object v1 = new object();

            var dictionary = new WeakKeyDictionary <object, object>();

            dictionary[k1] = v1;

            Assert.Equal(1, dictionary.Count);

            k1 = null;
            GC.Collect();

            dictionary.Scavenge();

            Assert.Equal(0, dictionary.Count);
        }