gpt4 book ai didi

c# - 方法本地 .NET 对象何时符合 GC 条件?

转载 作者:行者123 更新时间:2023-11-30 12:39:32 25 4
gpt4 key购买 nike

假设我有一个像这样的 C# 方法:(显然不是真正的代码)

byte[] foo()
{
var a = MethodThatReturns500mbObject();
var b = MethodThatReturns200mbObject(a);
byte[] c = MethodThatReturns150mbByteArray(b);
byte[] d = UnwiselyCopyThatHugeArray(c);
return d;
}

正如您可以从命名中猜到的那样,这些方法返回的对象是巨大的。每个对象总共需要数百兆字节的 RAM,尽管前两个对象由数百万个较小的对象组成,而不是像后两个数组那样由一个巨大的 block 组成。

我们很快会将其优化为流解决方案,但与此同时我想确保至少我们不会在执行代码以生成较晚对象时阻止较早对象的 GC。

我的问题是:will object a MethodThatReturns200mbObject(a) 返回后是否有资格进行 GC?如果没有,让 GC 知道有 500MB 的礼物在等待它的最佳方法是什么?

我的问题的核心是.NET GC 对“此对象没有引用”的判断是否足够聪明,可以知道 a MethodThatReturns200mbObject(a) 之后不能引用返回。尽管var a理论上仍可用于以后的代码,a在方法的第二行以下的任何地方都没有被引用。理论上,编译器可以让 GC 知道 a未被引用。但在实践中,我不确定它的行为方式。你知道吗?

最佳答案

This post explains it with examples.

理论上,编译器可以让 GC 知道 a 未被引用。但在实践中,我不确定它的行为方式。你知道吗?

The correct answer is that it depends on the project configuration whether the object will be eligible for garbage collection at the end of the method. As discussed in When do I need to use GC.KeepAlive? (which also describes the purpose of GC.KeepAlive – in short, it’s a way of referencing or “using” a variable making sure that the optimizer won’t optimize the usage away), the garbage collector might decide to collect objects as soon as they are not usable by any executing code anymore. This can very well happen in situations where it would be valid to access a reference (at compile time), but no such code has been written.

However, when compiling and executing code in Debug-mode, the compiler prevents this from happening to ease debugging. As a result, the correct implementation of our test method includes a preprocessor directive:

另一本好书 When do I need to use GC.KeepAlive?

关于c# - 方法本地 .NET 对象何时符合 GC 条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45227097/

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