跨越 AppDomain 边界的垃圾收集对象 [英] Garbage collecting objects crossing AppDomain boundary

查看:26
本文介绍了跨越 AppDomain 边界的垃圾收集对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您将继承自 MarshalByRefObject 的对象传递给不同的 AppDomain 时,由创建它的 AppDomain 诱导的 GC.Collect() 不会收集该对象,前提是该对象在 GC 时未植根于任一 AppDomain.Collect() 调用了吗?

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?

[当我说没有 root 时,我的意思是没有开发人员编写的代码可以访问它了.]

[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!

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

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?

谢谢各位

附言我知道代码中的 GC.Collect() 很糟糕,原因有很多,我只是想了解 GC 如何在 MBRO 上发生.

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.

推荐答案

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

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

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

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;

在 MSDN 的某个地方有一篇关于这个的很棒的文章(我现在找不到:/)

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

这篇关于跨越 AppDomain 边界的垃圾收集对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆