예제 #1
0
 protected override void Dispose(bool disposing)
 {
     _disposed = true;
     if (disposing)
     {
         _keepAlive.Clear();
         _cache.Clear();
     }
     base.Dispose(disposing);
 }
예제 #2
0
        public void TestKeepAliveMin()
        {
            WeakReference   r;
            ObjectKeepAlive keep = new ObjectKeepAlive(1, 10, TimeSpan.FromTicks(1), true);

            for (int i = 0; i < 35; i++)
            {
                keep.Add(i);
            }

            if (true)
            {
                object target = new MyObject();
                r = new WeakReference(target);
                keep.Add(target);
                target = null;
            }

            _destroyed = false;
            Assert.IsTrue(r.IsAlive);

            for (int i = 0; i < 100; i++)
            {
                keep.Tick();
            }

            Assert.IsTrue(r.IsAlive);

            GC.GetTotalMemory(true);
            GC.WaitForPendingFinalizers();
            Assert.IsTrue(r.IsAlive);
            Assert.IsFalse(_destroyed);

            System.Threading.Thread.Sleep(1);

            GC.GetTotalMemory(true);
            GC.WaitForPendingFinalizers();
            Assert.IsTrue(r.IsAlive);
            Assert.IsFalse(_destroyed);

            keep.Clear();

            GC.GetTotalMemory(true);
            GC.WaitForPendingFinalizers();
            Assert.IsFalse(r.IsAlive);
            Assert.IsTrue(_destroyed);
        }