I have a very simple application using EF. But when it runs a week, the memory usage is awful (only 80MB at first, 700MB after one week). When I use dotMemory to profile my application. I find the memory of Heap generation 2 is increasing all the time.
我有一个使用EF的非常简单的应用程序。但是当它运行一周时,内存使用率很差(一开始只有80MB,一周后只有700MB)。当我使用dotMemory来配置我的应用程序时。我发现Heap第2代的记忆一直在增加。
I Get a snapshot, finally find the retained bytes of ef dbcontext is the most.
我得到一个快照,最后发现ef dbcontext的保留字节是最多的。
I am so confused. My application is so simple. Code sample:
我感到很困惑。我的申请很简单。代码示例:
protected CarbonBrushMonitorEntities _entities = new MYEntities();
public void Add(HistoryData data)
{
_entities.HistoryDatas.Add(data);
_entities.SaveChanges();
}
_entities
only initials once at the starting time, then used all the time.
_entities只在开始时间初始化一次,然后一直使用。
The function Add
is frequently called,about 3 times/second
经常调用Add函数,大约3次/秒
I google a long time, and try some methods such as:
我谷歌很长一段时间,并尝试一些方法,如:
_entities.Configuration.ValidateOnSaveEnabled = false;
_entities.Configuration.AutoDetectChangesEnabled = false;
_entities.Configuration.LazyLoadingEnabled = false;
but these do not work.
但这些都行不通。
1 个解决方案
#1
If you use entity framework, you should create the context just before you need it and dispose it as soon as possible:
如果您使用实体框架,则应在需要之前创建上下文并尽快处理它:
using (var someContext = new SomeContext())
{
// your commands/queries
}
Never keep context in memory or share it across different calls.
切勿将内容保留在内存中或在不同的呼叫*享。
What I typically do is register the context with an IoC container:
我通常做的是使用IoC容器注册上下文:
DependencyFactory.RegisterType(typeof(SomeContext));
and use a context resolver (also registered with IoC of course) like:
并使用上下文解析器(当然也在IoC中注册),如:
using (var someContext = _contextResolver.ResolveContext())
{
// your commands/queries
}
where resolution is done like:
分辨率如下:
public class ContextResolver : IContextResolver
{
public ISomeContext ResolveContext()
{
return DependencyFactory.Resolve<SomeContext>();
}
}
The EF context is actually your unit of work, which should be disposed of once you don't need it anymore.
EF上下文实际上是您的工作单元,一旦您不再需要它就应该被处理掉。
#1
If you use entity framework, you should create the context just before you need it and dispose it as soon as possible:
如果您使用实体框架,则应在需要之前创建上下文并尽快处理它:
using (var someContext = new SomeContext())
{
// your commands/queries
}
Never keep context in memory or share it across different calls.
切勿将内容保留在内存中或在不同的呼叫*享。
What I typically do is register the context with an IoC container:
我通常做的是使用IoC容器注册上下文:
DependencyFactory.RegisterType(typeof(SomeContext));
and use a context resolver (also registered with IoC of course) like:
并使用上下文解析器(当然也在IoC中注册),如:
using (var someContext = _contextResolver.ResolveContext())
{
// your commands/queries
}
where resolution is done like:
分辨率如下:
public class ContextResolver : IContextResolver
{
public ISomeContext ResolveContext()
{
return DependencyFactory.Resolve<SomeContext>();
}
}
The EF context is actually your unit of work, which should be disposed of once you don't need it anymore.
EF上下文实际上是您的工作单元,一旦您不再需要它就应该被处理掉。