开发者

Instantiating a Spring.Net context inside a static method causes a memory leak

Ran across this recently and wondering if someone out there can give me a good explanation. I was doing some refactoring and created a spring context to grab a configured service from an embedded config file like so:

var myService = new XmlApplicationContext("assembly://MyAssembly/MyNamespace/MyService.config").GetObject( "myService") as MyService;

Normally this seems to work ok, but in this case, since it was inside a static method it caused a memory leak and I want to understand why so I avoid this sort of thing in the future. The method should not have been static in the first place, but that's another issue. Normally, I avoid doing this and inject my services directly onto an ASP.Net page via the web.config, but in this case it was expedient, so don't bother telling me I"m doing it all wrong, 开发者_JAVA技巧I know. ;-)

Max


If you have static method and you keep the myService instance handle, it'll be shared with Spring.NET's singleton map and you. As long as you have the reference the application context will also stick around. Though this doesn't fully explain the situation if the memory is constantly growing unless you keep every created object in your static class...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜