public void RecyclerPerf() { var sharedPool = new SharedPool <UnmanagedBuffer>(() => UnmanagedBuffer.Allocate(100), 1); var shared = sharedPool.GetOrCreate(); shared.Dispose(); Stopwatch sw = Stopwatch.StartNew(); int iterations = 10; for (int i = 0; i < iterations; i++) { sharedPool.TryGet(out shared); shared.Dispose(); } sw.Stop(); Console.WriteLine($"Get + Release = {sw.ElapsedMilliseconds * 1000000d / iterations} ns"); }
// [TestMethod, Timeout(60000)] public void RefCountedFinalizationTest() { var sharedPool = new SharedPool <UnmanagedBuffer>(() => UnmanagedBuffer.Allocate(100), 1); var shared = sharedPool.GetOrCreate(); var otherShared = shared.DeepClone(); shared = null; // after GC and finalization, the live copy is not affected GC.Collect(); GC.WaitForPendingFinalizers(); Assert.AreNotEqual(IntPtr.Zero, otherShared.Resource.Data); otherShared = null; // after GC and finalization of all live copies, the resource goes back to the pool without being finalized itself GC.Collect(); GC.WaitForPendingFinalizers(); var wasRecycled = sharedPool.TryGet(out shared); Assert.IsTrue(wasRecycled); Assert.IsNotNull(shared.Resource); Assert.AreNotEqual(IntPtr.Zero, shared.Resource.Data); }