public async Task DoForceGCAsync() { if (IsMeasuringCurrentProcess) { const int MAX_CLEANUP_CYCLES = 10; // Each time a GC occurs more COM objects can become available for cleanup. // So we keep calling until no more objects are available for cleanup OR // we reach a hard coded limit. for (int iLoopCount = 0; iLoopCount < MAX_CLEANUP_CYCLES; ++iLoopCount) { GC.Collect(GC.MaxGeneration); GC.WaitForPendingFinalizers(); if (!Marshal.AreComObjectsAvailableForCleanup()) { break; } Marshal.CleanupUnusedObjectsInCurrentContext(); } } else { // we just finished executing the user code. The IDE might be busy executing the last request. // we need to delay some or else System.Runtime.InteropServices.COMException (0x8001010A): The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER)) //await Task.Delay(TimeSpan.FromSeconds(1 * stressUtilOptions.DelayMultiplier)).ConfigureAwait(false); // cmdidShellForceGC GarbageCollectCLRIterative https://devdiv.visualstudio.com/DevDiv/_git/VS?path=%2Fsrc%2Fappid%2FAppDomainManager%2FVsRcwCleanup.cs&version=GBmaster&_a=contents await stressUtilOptions.VSHandler.DteExecuteCommand("Tools.ForceGC"); //await Task.Delay(TimeSpan.FromSeconds(1 * stressUtilOptions.DelayMultiplier)).ConfigureAwait(false); } }
static int Main(string[] doNotUse) { // RegFree COM is not supported on Windows Nano if (TestLibrary.Utilities.IsWindowsNanoServer) { return(100); } try { RunTests(); Console.WriteLine("Testing COM object lifetime control methods."); Thread.CurrentThread.DisableComObjectEagerCleanup(); RunTests(); Marshal.CleanupUnusedObjectsInCurrentContext(); Assert.IsFalse(Marshal.AreComObjectsAvailableForCleanup()); } catch (Exception e) { Console.WriteLine($"Test Failure: {e}"); return(101); } return(100); }
private void GarbageCleanup() { // Now let the GC clean up (repeat, until no more) do { GC.Collect(); GC.WaitForPendingFinalizers(); }while (Marshal.AreComObjectsAvailableForCleanup()); }
public void CleanupUnusedObjectsInCurrentContext_InvokeSeveralTimes_Success() { Marshal.CleanupUnusedObjectsInCurrentContext(); Assert.False(Marshal.AreComObjectsAvailableForCleanup()); // Invoke twice to make sure things work when unused objects have already been // cleaned up and there is nothing to do. Marshal.CleanupUnusedObjectsInCurrentContext(); Assert.False(Marshal.AreComObjectsAvailableForCleanup()); }
/// <summary> /// Try to call Marshal API for COM Types /// </summary> /// <returns></returns> static bool MarshalAPI() { Console.WriteLine("Scenario: MarshalAPI"); // MarshalAPI if (Marshal.AreComObjectsAvailableForCleanup()) { Console.WriteLine("AreComObjectsAvailableForCleanup should return false"); return(false); } return(true); }
static void DoMyExcelStuffAndCleanup() { DoMyExcelStuff(); // Now let the GC clean up (repeat, until no more) do { GC.Collect(); GC.WaitForPendingFinalizers(); }while (Marshal.AreComObjectsAvailableForCleanup()); }
private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { //isCancellationRequested = true; if (Marshal.AreComObjectsAvailableForCleanup()) { Marshal.CleanupUnusedObjectsInCurrentContext(); } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); }
static void Main(string[] args) { // NOTE: Don't call Excel objects in here... // Debugger would keep alive until end, preventing GC cleanup // Call a separate function that talks to Excel DoTheWork(); // Now let the GC clean up (repeat, until no more) do { GC.Collect(); GC.WaitForPendingFinalizers(); }while (Marshal.AreComObjectsAvailableForCleanup()); }
protected override void Dispose(bool disposing) { if (Marshal.AreComObjectsAvailableForCleanup()) { UnloadPreviewHandler(); } if (currentPreviewHandler != null) { Marshal.FinalReleaseComObject(currentPreviewHandler); currentPreviewHandler = null; GC.Collect(); } if (currentPreviewHandlerStream != null) { currentPreviewHandlerStream.Close(); currentPreviewHandlerStream = null; } base.Dispose(disposing); }
public void AreComObjectsAvailableForCleanup_NothingToCleanup_ReturnsFalse() { Assert.False(Marshal.AreComObjectsAvailableForCleanup()); }
public void TestCleanup() { Marshal.AreComObjectsAvailableForCleanup().Should().BeFalse(); }