GC.AddMemoryPressure() 如何知道要向哪个对象添加内存压力?
我最近需要使用 GC.AddMemoryPressure 和它让我感到奇怪的是,它不接受添加内存压力的对象作为参数。我认为因为它与运行时紧密相关,所以有某种机制可以将 this
指针传递给该方法。我的问题有三个:
- this 指针如何传递给方法?
- 我注意到从静态方法调用它时没有抛出异常。在这种情况下会发生什么?
- 为什么其他 GC 方法(例如 GC.SupressFinalize 和 GC.ReRegisterForFinalize)采用对象参数,而该方法不需要该参数?
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:
- How does the this pointer get passed to the method?
- I notice no exception is thrown when calling it from a static method. What happens in this case?
- Why do other GC methods such as GC.SupressFinalize and GC.ReRegisterForFinalize take an object argument where this method doesn't need one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,它没有与任何特定对象明确关联。假设在处理/完成时,同一个对象将消除该压力。来自文档:
在更复杂的场景中,内存压力可能会随着时间的推移而变化 - 但仍然预计会与相关对象的合作有关。
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:
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.
AddMemoryPressure 会比正常情况下更快地启动垃圾收集器。就这样。当实例被收集并且需要运行它的终结器时,抑制和 RegisterForFinialise 调用或不调用该类型的特定代码...
建议您阅读垃圾收集器伙伴,您可能会发现您不需要调用IncreaseMemoryPressure,或者调用它几乎同样可能会导致性能下降。
AddMemoryPressure kicks the garbage collector into life sooner than it would do normally. That's all. Suppress and RegisterForFinialise call or not specific code for that type when an instance gets collected and it's finaliser needs to be run...
Suggest you read up on the garbage collector mate, you might find that you didn't need to call IncreaseMemoryPressure, or almost just as likely calling it will cause a deteriation in performance.