views:

49

answers:

1

Hi all,

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, I know. ;-)

Max

A: 

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...

Marko Lahma