小编pwn*_*ess的帖子

使用语句/调用在静态方法中使用

我正在开发一个包含静态方法的实用程序类,这些方法经常被多个线程调用.我注意到内存使用随着时间的推移而增长,所以在我看来我有一些内存泄漏.

这是我用于这些静态方法的模式的一个简单示例:

public static int CreateNewThing(int IDofSomeDBObjectHoldingInfoToCreateNewThing)
{
    using(SomeDBContext context = new SomeDBContext(connectionString))
    {
        SomeDBObjectHoldingInfoToCreateNewThing createNewThingyInfo = context.SomeDBObjectsHoldingInfoToCreateNewThing.First(obj => obj.ID == IDofSomeDBObjectHoldingInfoToCreateNewThing);
        // Do some stuff to create the new object
        // Return the ID of the newly created thingy...
        return theNewThingThatWasCreated.ID;
     }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,使用'using'语句或直接调用静态方法中的Dispose实际上是否清除了任何内存.当然,我没有说明这个应用程序的整体架构或目的,但我也想知道我是否在这里使用最好的模式.在.NET中创建线程安全实用程序类的最佳实践是什么?

.net c# parallel-processing memory-leaks entity-framework

1
推荐指数
2
解决办法
2365
查看次数