예제 #1
0
        public void TestKeepAliveMax()
        {
            WeakReference r;
            ObjectKeepAlive keep = new ObjectKeepAlive(25, 50, TimeSpan.FromSeconds(1));

            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 < 49; i++)
                keep.Add(i);

            Assert.IsTrue(r.IsAlive);

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

            keep.Add(new object());
            keep.Add(new object());

            GC.GetTotalMemory(true);
            GC.WaitForPendingFinalizers();
            Assert.IsFalse(r.IsAlive);
            Assert.IsTrue(_destroyed);
        }
예제 #2
0
        public void TestTruncateLarge()
        {
            TimeSpan timeout = TimeSpan.FromMilliseconds(25);
            ObjectKeepAlive keep = new ObjectKeepAlive(100, 10000, timeout);

            for (int i = 0; i < 100000; i++)
                keep.Add(i);

            System.Threading.Thread.Sleep(timeout);

            for (int i = 0; i < 10000; i++)
                keep.Add(i);
        }
예제 #3
0
        public void TestKeepAliveTime()
        {
            WeakReference   r;
            TimeSpan        timeout = TimeSpan.FromMilliseconds(100);
            ObjectKeepAlive keep    = new ObjectKeepAlive(1, 10, timeout, false);

            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 < 5; i++)
            {
                keep.Add(i);
            }

            Assert.IsTrue(r.IsAlive);

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

            long start = DateTime.UtcNow.Ticks;

            while ((start + timeout.Ticks) > DateTime.UtcNow.Ticks)
            {
                System.Threading.Thread.SpinWait(100);
            }

            //Time has elapsed, yet it nothing is added, and Tick() is not called, it remains in memory
            GC.GetTotalMemory(true);
            GC.WaitForPendingFinalizers();
            Assert.IsTrue(r.IsAlive);
            Assert.IsFalse(_destroyed);

            //Once the collection is touched with either a call to Add or Tick, timeout will expire
            keep.Add(new object());

            GC.GetTotalMemory(true);
            GC.WaitForPendingFinalizers();
            Assert.IsFalse(r.IsAlive);
            Assert.IsTrue(_destroyed);
        }
예제 #4
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);
        }
예제 #5
0
            CacheEntry GetCache(NodeHandle handle, bool isNew)
            {
                WeakReference <CacheEntry> weakRef;
                CacheEntry entry = null;

                if (!isNew)
                {
                    if (handle.TryGetCache(out weakRef) && weakRef != null && weakRef.TryGetTarget(out entry))
                    {
                        return(entry);
                    }

                    using (_cacheLock.Read(base.Options.LockTimeout))
                    {
                        if (_cache.TryGetValue(handle, out weakRef))
                        {
                            if (!weakRef.TryGetTarget(out entry))
                            {
                                using (new SafeLock <DeadlockException>(weakRef))
                                {
                                    if (!weakRef.TryGetTarget(out entry))
                                    {
                                        weakRef.Target = entry = new CacheEntry(this, handle);
                                    }
                                    handle.SetCacheEntry(weakRef);
                                }
                            }
                        }
                    }
                }
                if (entry == null)
                {
                    using (_cacheLock.Write(base.Options.LockTimeout))
                    {
                        if (!_cache.TryGetValue(handle, out weakRef))
                        {
                            _cache.Add(handle, weakRef = new WeakReference <CacheEntry>(entry = new CacheEntry(this, handle)));
                            handle.SetCacheEntry(weakRef);
                        }
                        else
                        {
                            if (!weakRef.TryGetTarget(out entry))
                            {
                                using (new SafeLock <DeadlockException>(weakRef))
                                {
                                    if (!weakRef.TryGetTarget(out entry))
                                    {
                                        weakRef.Target = entry = new CacheEntry(this, handle);
                                    }
                                    handle.SetCacheEntry(weakRef);
                                }
                            }
                        }
                    }
                }
                Assert(entry != null, "Cache entry is null");
                _keepAlive.Add(entry);
                return(entry);
            }