Пример #1
0
        public void B_TestOutOfMemory()
        {
            var wht       = TestWeakDictionaryBehavior.CreateDictionary();
            int OOMECount = 0;

            for (int i = 0; i < 1024 * 24 + 32; i++) // total requested Mem. > 24GB
            {
                try
                {
                    wht.Add(new BigObject(i), i);
                    if (i % 1024 == 0)
                    {
                        Console.WriteLine("Requested Mem: " + i.ToString() + " MB");
                    }
                    OOMECount = 0;
                }
                catch (OutOfMemoryException oom)
                {
                    if (OOMECount++ > 10)
                    {
                        throw new Exception("Memory Allocation Error in B_TestOutOfMemory");
                    }
                    //Try Again. GC will eventually release some memory.
                    Console.WriteLine("OOME WHEN i=" + i.ToString() + ". Try Again");
                    System.Threading.Thread.Sleep(10);
                    i--;
                    continue;
                }
            }

            GC.Collect();
            Console.WriteLine("Passed out of memory exception.");
        }
Пример #2
0
        public void A_TestBasicOps()
        {
            ConditionalWeakTableWrapper <object, object> weakDictionary = TestWeakDictionaryBehavior.CreateDictionary();// new SupportClass.TjWeakHashTable();
            Hashtable realHashTable = new Hashtable();

            SmallObject[] so = new SmallObject[100];
            for (int i = 0; i < 20000; i++)
            {
                SmallObject key   = new SmallObject(i);
                SmallObject value = key;
                so[i / 200] = key;
                realHashTable.Add(key, value);
                weakDictionary.Add(key, value);
            }

            Assert.AreEqual(weakDictionary.Count, realHashTable.Count);

            var keys = realHashTable.Keys;

            foreach (SmallObject key in keys)
            {
                Assert.AreEqual(((SmallObject)realHashTable[key]).i,
                                ((SmallObject)weakDictionary[key]).i);

                Assert.IsTrue(realHashTable[key].Equals(weakDictionary[key]));
            }


            var values1 = weakDictionary.Values;
            var values2 = realHashTable.Values;

            Assert.AreEqual(values1.Length, values2.Count);

            realHashTable.Remove(new SmallObject(10000));
            weakDictionary.Remove(new SmallObject(10000));
            Assert.AreEqual(weakDictionary.Count, 20000);
            Assert.AreEqual(realHashTable.Count, 20000);

            for (int i = 0; i < so.Length; i++)
            {
                realHashTable.Remove(so[i]);
                weakDictionary.Remove(so[i]);
                Assert.AreEqual(weakDictionary.Count, 20000 - i - 1);
                Assert.AreEqual(realHashTable.Count, 20000 - i - 1);
            }

            //After removals, compare the collections again.
            var keys2 = realHashTable.Keys;

            foreach (SmallObject o in keys2)
            {
                Assert.AreEqual(((SmallObject)realHashTable[o]).i,
                                ((SmallObject)weakDictionary[o]).i);
                Assert.IsTrue(realHashTable[o].Equals(weakDictionary[o]));
            }
        }
Пример #3
0
        public void C_TestMemLeakage()
        {
            var wht = TestWeakDictionaryBehavior.CreateDictionary(); //new SupportClass.TjWeakHashTable();

            GC.Collect();
            int initialMemUsage = GetMemUsageInKB();

            Console.WriteLine("Initial MemUsage=" + initialMemUsage);
            for (int i = 0; i < 10000; i++)
            {
                wht.Add(new BigObject(i), i);
                if (i % 100 == 0)
                {
                    int mu = GetMemUsageInKB();
                    Console.WriteLine(i.ToString() + ") MemUsage=" + mu);
                }
            }

            GC.Collect();
            int memUsage = GetMemUsageInKB();

            Assert.IsFalse(memUsage > initialMemUsage * 2, "Memory Leakage.MemUsage = " + memUsage);
        }
 public void Setup()
 {
     dictionary = TestWeakDictionaryBehavior.CreateDictionary();
 }