简体   繁体   English

Windows服务中的内存泄漏-InterceptableDbCommand

[英]Memory leak in Windows Service - InterceptableDbCommand

I've got problem with memory leak. 我的内存泄漏有问题。 When I run my application in window mode, everything is OK, but when I host it as a Windows Service there is a problem with memory leak. 当我在窗口模式下运行应用程序时,一切正常,但是当我将其作为Windows服务托管时,内存泄漏会出现问题。 It's not releasing DbContext and another objects because they are referenced to InterceptableDbCommand which is GC Root. 它不会释放DbContext和其他对象,因为它们被引用为GC根的InterceptableDbCommand。 InterceptableDbCommand contains queries to database generated by Entity Framework. InterceptableDbCommand包含对由实体框架生成的数据库的查询。

What is diffrience in relasing memory between application and windows service mode? 应用程序和Windows服务模式之间的重新分配内存有何区别?

Generally Garbage Collector releases memory if its 'Gen 0' going to overflow (Large object heap also can trigger GC). 通常,如果垃圾回收器的“ Gen 0”即将溢出(大对象堆也可以触发GC),则会释放内存。 AFAIK Garbage Collector uses the same approach to release the memory for both .Net Applications and Windows Services. AFAIK垃圾收集器使用相同的方法来释放.Net应用程序和Windows服务的内存。

If there is a memory leak probably it should exists in both Application and Windows Service (if the workflow is exactly the same). 如果存在内存泄漏,则可能在应用程序和Windows服务中都存在(如果工作流程完全相同)。 May be you have the same issue with the Application, but you don't notice because of its workflow nature. 可能您的应用程序存在相同的问题,但是由于其工作流程的性质而没有引起注意。 How did you test it for memory leaks? 您如何测试它的内存泄漏?

More importantly: Make sure you dispose InterceptableDbCommand and all other disposable objects. 更重要的是:确保处置InterceptableDbCommand和所有其他一次性对象。

Ok I found cause. 好的,我找到了原因。 There was attribute [STAThread] in Main method, after deleting it everything works just fine. Main方法中有属性[STAThread],将其删除后,一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM