是什么使委托多目标对象保留在范围内?

发布于 2024-12-22 23:01:05 字数 404 浏览 3 评论 0原文

是什么使 t 成为根引用(留在范围内)? (t是一个用户定义的类)

我在IL间谍中查看,它不是一个常见的捕获变量!

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);

有人可以向我解释一下吗? t(任务)类如何保持在范围内,是什么让它扎根,我猜它是运行委托,但是如何呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

同尘 2024-12-29 23:01:05

首先,我应该指出,这与范围没有太大关系,范围是程序文本的区域,在该区域中可以使用实体的简单名称来引用实体。这是关于可达性

现在我还没有使用内存分析器查看相关堆,但是 Task 对象的路径看起来像这样:

  1. Action(多播)委托实例是 GC 根,因为它由 runs (本地)引用。
  2. 该委托实例又通过其 _invocablelist 字段保持其各个订阅者的活动状态,该字段保存对委托数组的引用。
  3. 该数组又包含对各个(单播)Action 委托实例的引用。
  4. 这些单独的委托中的每一个都将是一个封闭实例委托,带有填充的_target字段(它将充当传递给this的引用) code>Task.Run 方法(当调用委托时)。该字段将保存对 Task 实例的引用。

总结一下:

    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

更新:

我通过Ants Memory Profiler,证实了我的想法:

Path to Task object

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:

  1. The Action (multicast) delegate instance is a GC root since it is referenced by runs (a local).
  2. This delegate instance in turn keeps its individual subscribers alive through its _invocationlist field, which holds a reference to a delegate array.
  3. This array in turn contains references to the individual (unicast) Action delegate instances.
  4. Each of these individual delegates will be a closed-instance delegate with a populated _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:

Path to Task object

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文