问题
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.
回答1:
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 :/)
来源:https://stackoverflow.com/questions/3956792/garbage-collecting-objects-crossing-appdomain-boundary