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 a Task object would look something like this:
Action (multicast) delegate instance is a GC root since it is referenced by runs (a local). _invocationlist field, which holds a reference to a delegate array.Action delegate instances. _target field (which will act as the this reference passed to the Task.Run method when the delegate is invoked). This field will hold a reference to the Task instance.To summarize:
runs local
-> Multicast Action object
-> (through _invocationlist field) Array of references to Action objects
-> (through a specific array element) Unicast Action object
-> (through _target field) Task object
UPDATE:
I ran this through Ants Memory Profiler, which confirmed my thoughts:

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With