非托管对象的 GC.AddMemoryPressure

发布于 2024-10-04 15:35:34 字数 156 浏览 1 评论 0原文

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

What is the correct way to use Add/RemoveMemoryPressure for unmanaged objects? I have an unmanaged object- but it makes heavy use of, e.g., STL containers. Am I supposed to constantly update the managed GC with every change to the total size of the unmanaged object? Or just it's raw allocation size? And when do I call RemoveMemoryPressure? Finalizer? Dispose()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咋地 2024-10-11 15:35:34

来自此处(并由 @Wim Coenen 注明):

在最简单的使用模式中,
托管对象分配非托管对象
构造函数中的内存并释放
它在 Dispose 或 Finalize 方法中。
调用AddMemoryPressure方法
分配非托管内存后,
并调用RemoveMemoryPressure
释放后的方法。

在更复杂的场景中,
非托管内存分配
期间发生重大变化
被管理对象的生命周期,
可以调用 AddMemoryPressure 和
删除内存压力方法
传达这些增量变化
到运行时

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

From here (and noted by @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
.

So you have to decide how much effort to put into tracking these changes and how much benefit you will get.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文