GC.AddMemoryPressure()如何知道向哪个对象添加内存压力? [英] How does GC.AddMemoryPressure() know which object to add memory pressure to?

查看:167
本文介绍了GC.AddMemoryPressure()如何知道向哪个对象添加内存压力?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近需要使用 GC.AddMemoryPressure 令我感到惊讶的是,它不接受将增加内存压力的对象作为参数.我假设因为它与运行时紧密相关,所以有某种机制可以将this指针传递给该方法.我的问题是三个方面:

I recently had need to use GC.AddMemoryPressure and it struck me as strange that it doesn't accept the object to add memory pressure to as an argument. I assume because it's so closely tied to the runtime, there is some mechanism by which the this pointer gets passed to the method. My question is threefold:

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

推荐答案

否,它未与任何特定对象明确关联.假设在处理/完成时间,相同的对象将消除该压力.来自文档:

No, it's not explicitly associated with any specific object. The assumption is that at disposal/finalize time, the same object will remove that pressure. From the docs:

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

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, the memory pressure may change over time - but it's still expected to be with the co-operation of the object in question.

这篇关于GC.AddMemoryPressure()如何知道向哪个对象添加内存压力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆