为什么温莎城堡守住瞬时对象?(Why does Castle Windsor hold onto t

2019-07-03 16:45发布

Recently I noticed my application appears to be eating memory that never gets released. After profiling with CLRProfiler I've found that the Castle Windsor container I'm using is holding onto objects. These objects are declared with the lifestyle="transient" attribute in the config xml.

I've found if I put an explicit call to IWindsorContainer.Release(hangingObject), that it will drop its references.

This is causing a problem though, I wasn't expecting that with a transient lifestyle object CastleWindsor would keep a reference and effectively create a leak. It's going to be a rather mundane and error prone task going around inserting explicit Release calls in all the appropriate places.

Have you seen this problem, and do you have any suggestions for how to get around it?

Answer 1:

我认为这里的答案是缺少一种关键的一点-这种行为通过发布政策是可配置的开箱-查看城堡项目站点上的文档在这里 。

在许多情况下尤其是当你的容器存在宿主应用程序的生命周期,并在短暂的成分真的不需要被跟踪(因为你正在处理处置在您的调用代码或组件的被注入的服务),那么你可以只设置发布政策到NoTrackingReleasePolicy实施,用它做。

此前城堡1.0版,我相信组件负担将实行/出台 - 这将有助于缓解这些问题,以及围绕依赖注入等的处置

编辑:

看看下面的职位的分量负担更多的讨论。

该组件负担-戴维Brions

此外组件负担在实现官方2.0版本的温莎集装箱。



Answer 2:

有一点需要注意的是,这似乎已经固定在城堡干线。 在r5475,哈米特更改了默认发布政策微内核到LifecycledComponentsReleasePolicy



Answer 3:

你可以尽管在容器对象上设置单或短暂的生活方式。 单一对象我的理解应该持续应用的生活,但我不明白这behvaviour的用处是瞬态的人一样!

自定义的生活方式可以通过实现创建ILifestyleManager 。 也许这是可以实现这个适当创建ReallyTransient生活方式的类型!



文章来源: Why does Castle Windsor hold onto transient objects?