public void VerifyCallbackWrappersCollectsUnusedWrappers() { DateTime endTime = DateTime.Now + TimeSpan.FromSeconds(19); var callbackWrappers = new CallbackWrappers(); RunFullGarbageCollection(); long memoryAtStart = EseInteropTestHelper.GCGetTotalMemory(true); while (DateTime.Now < endTime) { for (int i = 0; i < 128; ++i) { CreateCallbackWrapper(callbackWrappers); } RunFullGarbageCollection(); callbackWrappers.Collect(); RunFullGarbageCollection(); } RunFullGarbageCollection(); long memoryAtEnd = EseInteropTestHelper.GCGetTotalMemory(true); GC.KeepAlive(callbackWrappers); long memory = memoryAtEnd - memoryAtStart; EseInteropTestHelper.ConsoleWriteLine("{0:N0} bytes used", memory); Assert.IsTrue(memory < 1024 * 1024, "Test used too much memory. JetCallbackWrapper objects weren't collected."); }
/// <summary> /// Perform an action, checking the system's memory usage before and after. /// </summary> /// <param name="action">The action to perform.</param> private static void CheckMemoryUsage(Action action) { RunGarbageCollection(); long memoryAtStart = EseInteropTestHelper.GCGetTotalMemory(true); int collectionCountAtStart = EseInteropTestHelper.GCCollectionCount(0); action(); int collectionCountAtEnd = EseInteropTestHelper.GCCollectionCount(0); RunGarbageCollection(); long memoryAtEnd = EseInteropTestHelper.GCGetTotalMemory(true); EseInteropTestHelper.ConsoleWriteLine( "Memory changed by {0:N} bytes ({1} GC cycles)", memoryAtEnd - memoryAtStart, collectionCountAtEnd - collectionCountAtStart); }