Suppose I have a C # method: (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 from the naming conventions, the objects returned by these methods are gigantic. Hundreds of megabytes of shared RAM are required by everyone, although the first two objects are made up of millions of smaller objects instead of one huge chunk, such as the last two arrays.
We are going to optimize this into a streaming solution in the near future, but at the same time I would like to make sure that at least we do not prevent the GC of previous objects when executing the code to create later objects.
My question is this: will the object abe eligible for the GC as soon as MethodThatReturns200mbObject (a) returns? If not, what is the best way to let the GC know what 500 MB is waiting for there?
The core of my question is whether the GC GC definition of “this object has no references” is smart enough to know that ayou cannot reference after returning MethodThatReturns200mbObject(a). Although var astill theoretically available for later code, it is anot referenced anywhere below the second line of the method. Theoretically, the compiler could tell GC what is anot specified. But in practice, I'm not sure how he behaves. Did you know?