Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Castle Windsor: Will my transient component be garbage collected?

Using Castle Windsor, I have a component configured with the transient lifestyle:

<component id="publish.mapping.default"
                   service="IMyService, MyAssembly"
                   type="MyServiceImplementation, Myassembly" 
                   lifestyle="transient" />

Which will be used like this:

var service = container.Resolve<IMyService>(componentId);
// service usage ....
// service goes out of scope ... 

My question is, will the service instance be garbage collected after it goes out of scope, or will Castle Windsor hold on to a reference ? I found this similar question, that implies that the latter might be the case - but after examining the links posted there, I am unsure whether the discussion is about holding on to the reference, or about ensuring that the object are disposed if it implements IDisposable. My objects does not need to be disposed.

If Castle Windsor holds on to the instance, is there any easy way to prevent this (perhaps by configuration) ?

EDIT
It seems, that I need to set the release tracking policy. Can this be configured in the xml config file, or does it need to be set in code ? Can the release tracking policy be set on a per-component basis ?

like image 479
driis Avatar asked Feb 23 '09 17:02

driis


2 Answers

By default, the container holds a reference to your objects (even the transient ones).

However, as @Bittercoder notes in Why does Castle Windsor hold onto transient objects?, you can change the release tracking policy. It seems that choosing

LifecycledComponentsReleasePolicy:

var policy = container.Kernel.ReleasePolicy;
container.Kernel.ReleasePolicy = LifecycledComponentsReleasePolicy;

But since the question was asked, that appears to have become the default policy.

like image 54
Blair Conrad Avatar answered Sep 23 '22 07:09

Blair Conrad


One thing to note is that this seems to have been fixed in the Castle Trunk. In r5475, Hammett changed the default release policy in MicroKernel to LifecycledComponentsReleasePolicy.

like image 30
Craig Vermeer Avatar answered Sep 25 '22 07:09

Craig Vermeer