.net – GC.AddMemoryPressure()如何知道添加内存压力的对象?

我最近需要使用
GC.AddMemoryPressure并且让我感到奇怪的是它不接受添加内存压力的对象作为参数.我假设因为它与运行时紧密相关,所以有一些机制可以将this指针传递给方法.我的问题有三个:

> this指针如何传递给方法?
>我注意到从静态方法调用它时不会抛出任何异常.在这种情况下会发生什么?
>为什么其他GC方法(如GC.SupressFinalize和GC.ReRegisterForFinalize)采用此方法不需要的对象参数?

最佳答案 不,它没有与任何特定对象明确关联.假设在处置/完成时间,同一个对象将消除该压力.从
the docs开始:

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.

在更复杂的情况下,内存压力可能会随着时间的推移而发生变化 – 但是仍然可以预期相关对象的合作.

点赞