gpt4 book ai didi

c# - 非托管对象的 GC.AddMemoryPressure

转载 作者:太空狗 更新时间:2023-10-29 21:55:46 27 4
gpt4 key购买 nike

对非托管对象使用 Add/RemoveMemoryPressure 的正确方法是什么?我有一个非托管对象 - 但它大量使用了例如 STL 容器。我是否应该随着非托管对象总大小的每次变化不断更新托管 GC?或者只是它的原始分配大小?我什么时候调用 RemoveMemoryPressure?终结者?处置()?

最佳答案

来自 here (并由@Wim Coenen 指出):

In the simplest usage pattern, a managed object allocates unmanaged memory in the constructor and releases it in the Dispose or Finalize method. Call the AddMemoryPressure method after allocating the unmanaged memory, and call the RemoveMemoryPressure method after releasing it.

In more complicated scenarios, where the unmanaged memory allocation changes substantially during the lifetime of the managed object, you can call the AddMemoryPressure and RemoveMemoryPressure methods to communicate these incremental changes to the runtime.

因此,您必须决定投入多少精力来跟踪这些变化,以及您将获得多少 yield 。

关于c# - 非托管对象的 GC.AddMemoryPressure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4281418/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com