What makes t a rooted reference (stay in scope) ? (t is a user defined class)
I look at in in IL spy, and its not a common capture variable !
Action runs = null;
while (dummy <= tod.Value.Date)
{
var t = new Task(dummy, _interval);
runs += t.Run;
dummy = dummy.AddDays(1);
}
GC.Collect();
((Action)(() => { runs(); })).BeginInvoke(Result, null);
Can someone explain this to me? How the t (task) classes stay in scope, what makes it rooted, I guess its the runs delegate, but how?
Firstly, I should point out that this doesn’t have much to with scope, which is the region of program text in which one can refer to an entity using its simple name. It’s about reachability.
Now I haven’t looked at the heap in question with a memory profiler, but the path to aTaskobject would look something like this:Action(multicast) delegate instance is a GC root since it is referenced byruns(a local)._invocationlistfield, which holds a reference to a delegate array.Actiondelegate instances._targetfield (which will act as thethisreference passed to theTask.Runmethod when the delegate is invoked). This field will hold a reference to theTaskinstance.To summarize:
UPDATE:
I ran this through Ants Memory Profiler, which confirmed my thoughts: