跨越 AppDomain 边界的垃圾收集对象
当您将从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您提到的,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 :/)