gpt4 book ai didi

c# - 将 GC.AddMemoryPressure 与非托管资源一起使用有什么意义?

转载 作者:可可西里 更新时间:2023-11-01 07:54:18 25 4
gpt4 key购买 nike

我在 MSDN 和 CLR via c# 上读到了这个问题。

假设我们有一个分配的 2Mb 非托管 HBITMAP 和一个指向它的 8 字节托管位图。如果它永远无法对对象做任何事情,那么用 AddMemoryPressure 告诉 GC 有什么意义,因为它被分配为非托管资源,因此不易受到垃圾收集的影响?

最佳答案

提供它是为了让 GC 在收集期间了解对象的真实成本。如果对象实际上大于管理大小反射(reflect)的大小,则它可能是快速(更)收集的候选对象。

布拉德·艾布拉姆斯 entry关于它很清楚:

Consider a class that has a very small managed instance size but holds a pointer to a very large chunk of unmanaged memory. Even after no one is referencing the managed instance it could stay alive for a while because the GC sees only the managed instance size it does not think it is “worth it” to free the instance. So we need to “teach” the GC about the true cost of this instance so that it will accurately know when to kick of a collection to free up more memory in the process.

关于c# - 将 GC.AddMemoryPressure 与非托管资源一起使用有什么意义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1149181/

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