Assume I have a C# method like this: (obviously not real code)
byte[] foo()
{
var a = MethodThatReturns500mbObject();
var b = MethodThatReturns200mbObject(a);
byte[] c = MethodThatReturns150mbByteArray(b);
byte[] d = UnwiselyCopyThatHugeArray(c);
return d;
}
As you can guess by the naming, the objects that are returned by these methods are gigantic. Hundreds of megabytes total RAM required by each, although the first two objects are composed of millions of smaller objects instead of one huge chunk like the latter two arrays.
We're going to optimize this into a streaming solution soon, but in the meantime I'd like to make sure that at least we're not preventing GC of the earlier objects while executing code to produce the later objects.
My question is this: will object a
be eligible for GC as soon as MethodThatReturns200mbObject(a) returns? If not, what's the best way to let the GC know that there's a 500MB present waiting for it?
The core of my question is whether the .NET GC's determination of "this object has no references" is smart enough to know that a
cannot be referenced after MethodThatReturns200mbObject(a)
returns. Even though var a
is still theoretically available to later code, a
is not referenced anywhere below the second line of the method. In theory, the compiler could let the GC know that a
is unreferenced. But in practice, I'm not sure how it behaves. Do you know?