zoukankan      html  css  js  c++  java
  • AreComObjectsAvailableForCleanup and CleanupUnusedObjectsInCurrentContext

    AreComObjectsAvailableForCleanup
    http://msdn.microsoft.com/zh-cn/library/system.runtime.interopservices.marshal.arecomobjectsavailableforcleanup.aspx

    Indicates whether runtime callable wrappers (RCWs) from any context are available for cleanup.

    Remarks

    If there are a lot of references between managed and native code with deep dependency graphs it can take a long time for all the objects to clean up.Each time a GC runs it will free up some number of RCWs, which will in turn release the underlying COM objects.Those COM objects will then release their managed references and make more objects available for cleanup the next time a GC runs, which starts the process over again.

    The AreComObjectsAvailableForCleanup method provides a way for the application to determine how many cycles of GC.Collect and GC.WaitForPendingFinalizers need to happen in order to clean everything up.

    Marshal.CleanupUnusedObjectsInCurrentContext
    (http://msdn.microsoft.com/zh-cn/library/system.runtime.interopservices.marshal.cleanupunusedobjectsincurrentcontext.aspx)

    Notifies the runtime to clean up all Runtime Callable Wrappers (RCWs) allocated in the current context.

    Remarks

    The interop system pumps messages while it attempts to clean up RCWs.This ensures that cleanup for single-threaded apartment (STA) RCWs happens in all applications, including applications that do not pump often enough.However, some applications that do pump appropriately are very sensitive to where pumping occurs.When the interop system pumps messages in these applications, in places that are unexpected, , the application can encounter reentrancy issues that are difficult to diagnose.Therefore, applications need to be able to control their own pumping for a particular STA and to control the clean-up of RCWs.

    However, developers often only control a subset of the threads in their application and thus cannot ensure that all threads are pumping appropriately.

    To solve this problem:

    1. Use the Thread.DisableComObjectEagerCleanup method to turn off automatic cleanup of RCWs, and the message pumping that occurs, on a per-thread basis.This allows developers to opt out of automatic clean-up, and the corresponding message pumping.

    2. Use the CleanupUnusedObjectsInCurrentContext method to notify the runtime to clean up all RCWs that are allocated in the current context.This companion method allows developers to precisely control when the runtime performs cleanup in the current context.

  • 相关阅读:
    【转】Quartz Cron 触发器 Cron Expression 的格式
    [转]MYSQL同时LEFT JOIN 多个表一例
    collapse用法
    flavor用法
    horny
    ever since用法
    be headed for用法
    Lemme用法
    scary用法
    feel like用法
  • 原文地址:https://www.cnblogs.com/xiaokang088/p/2944212.html
Copyright © 2011-2022 走看看