跨越 AppDomain 边界的垃圾收集对象

发布于 2024-09-27 17:11:09 字数 377 浏览 9 评论 0原文

当您将从 MarshalByRefObject 继承的对象传递给不同的 AppDomain 时,创建该对象的 AppDomain 引发的 GC.Collect() 不会收集该对象,前提是该对象在 GC.Collect( 时未植根于任一 AppDomain 中) )叫?

[当我说没有 root 时,我的意思是不再有开发人员编写的代码访问它。]

看来该对象没有被收集,而是被提升到下一代!

但是,如果我停止将对象传递给 diff AppDomain,它就会按预期被收集。

这是设计使然吗?如果是的话有什么道理吗?

谢谢大家,

PS 我知道代码中的 GC.Collect() 出于多种原因是不好的,我只是想了解 GC 如何在 MBRO 上发生。

When you pass an object that inherits from MarshalByRefObject to a different AppDomain, won't GC.Collect() induced by the AppDomain that created it collect the object, provided that the object is not rooted in either AppDomain by the time GC.Collect() called?

[When I say not rooted I mean no developer written code access it anymore.]

It appears that the object is not getting collected but rather getting promoted to the next generation!

But if I stop passing the object to a diff AppDomain, it is getting collected as expected.

Is this behavior by design? If so rationale?

Thanks guys,

P.S. I know GC.Collect() within code is bad for many reasons, I'm just trying to understand how GC would take place on MBROs.

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

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

发布评论

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

评论(1

雨落□心尘 2024-10-04 17:11:09

正如您提到的,GC 很难跟踪 MBRO 对象。所以 MS 的行为有点不同。
这些对象有两个属性:它们的初始生命周期(我认为是五分钟)和 RenewOnCallTime(两分钟)。如果创建了 MBRO 对象,它就有其初始生命周期。一旦这个时间为零,它就会被标记为GC。
对对象的每次调用都允许对象的 RenewOnCallTime 生存时间更长(如果剩余生存期小于 RenewOnCallTime)。

例如(5 分钟初始生命周期,2 分钟 RenewOnCallTime):
对象创建:生命周期为五分钟;
4分钟过去了;生命周期是一分钟;
调用对象;生命周期是两分钟;
2 分钟过去了;
对象被标记为GC,没有剩余生命周期;

MSDN 上有一篇关于此的很棒的文章(我现在找不到:/)

As you mentioned MBRO objects are hard to keep track of for the gc. So MS implemented their behaviour a bit different.
Those objects have two properties: Their initial lifetime (I think five minutes) and a RenewOnCallTime (two minutes). If a MBRO object is created it has it's initial lifetime. Once this time is zero it is marked for gc.
Every call on the object allows the object to live for RenewOnCallTime longer (if the remaining lifetime is less then the RenewOnCallTime).

For an example (5 minutes initial lifetime, 2 minutes RenewOnCallTime):
Object is created: Lifetime is five minutes;
4 minutes pass; Lifetime is one minute;
Call to object is made; Lifetime is two minutes;
2 minutes pass;
Object is marked for gc, No Lifetime left;

Somewhere on MSDN there is a great article about this (which I can't find right now :/)

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