在静态方法中实例化 Spring.Net 上下文会导致内存泄漏

发布于 2024-08-09 04:24:53 字数 440 浏览 4 评论 0原文

最近遇到这个问题,想知道是否有人可以给我一个很好的解释。我正在进行一些重构并创建了一个 spring 上下文来从嵌入式配置文件中获取配置的服务,如下所示:

var myService = 新的 XmlApplicationContext("程序集://MyAssembly/MyNamespace/MyService.config").GetObject( "myService") as MyService;

通常这似乎工作正常,但在这种情况下,由于它在静态方法中,所以导致了内存泄漏,我想了解为什么,所以我在未来。该方法一开始就不应该是静态的,但这是另一个问题。通常,我会避免这样做,而是通过 web.config 将我的服务直接注入到 ASP.Net 页面上,但在本例中这是权宜之计,所以不要费心告诉我我做错了,我知道。; -)

最大限度

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪操控生活 2024-08-16 04:24:53

如果您有静态方法并且保留 myService 实例句柄,它将与 Spring.NET 的单例映射和您共享。只要您有引用,应用程序上下文就会保留下来。尽管这并不能完全解释内存不断增长的情况,除非您将每个创建的对象保留在静态类中......

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文