Avoid Memory Leaks When Using Windsor And Not Releasing Objects

I just had this issue in a project I am working on (ASP.NET Webforms Application), yesterday I ran a load test and watched my memory climb rapidly and max out not long after. I am using Castle Windsor for my IOC and mostly transient instances are registered so I did not expect any issues here as I asssumed there would be no references held to my transient instances but the leak shows this is happening.

I am not releasing my services once done so this is partly my fault but I do not wan't to explicitly release them so I found another way which means setting the ReleasePolicy on the kernel to use the NoTrackingReleasePolicy. The default release policy is the AllComponentsReleasePolicy which from what I can see by debugging will keep track of your instances and you will need to release them manually when you are finished with the item.

Simple Fix Using NoTrackingReleasePolicy

To use this when creating your kernel set the array resolver like so to use the NoTrackingReleasePolicy for its release policy.

// Create.
IKernel kernel = new DefaultKernel();

// Set release policy.
kernel.ReleasePolicy = new NoTrackingReleasePolicy();

Now my application does not leak and is stable. I am not 100% if this is the best way to handle this and if I should actually release but it is working a charm. Funny to think what would have happened if I never checked memory use like I know happens out there :\ would have been interested, and all it took was something simple to fix.



1 Comment

Comments have been disabled for this content.