0

I have a multi threading console application that written in C# and Entity Framework 4.0. Yesterday, when I monitored the program for couple hours, it uses about 100MB, but this morning it becomes 500MB. Just wondering if EF will eventually become big because of caching? Or should I somehow refresh it?

Thanks in advance.

Dreteh
  • 387
  • 2
  • 4
  • 11

3 Answers3

0

Unless you have a valid reason to monitor memory usage, there's typically none. Garbage collector will kick in when there's a need for memory and will empty unused one.

As long as you are not keeping references to non-used objects somewhere, memory usage should not be a concern in garbage collected environments.

Jcl
  • 27,696
  • 5
  • 61
  • 92
  • That said, there are *lots* of ways you could keep unused references and prevent them from being collected. A memory profiler might help (like [this](http://memprofiler.com/) or [this](http://www.jetbrains.com/profiler/)) – Jcl May 30 '12 at 05:46
  • I am afraid objects that cached in the EF will not be collected by Garbage Collector. – Dreteh May 30 '12 at 05:57
  • 1
    They will, as soon as you dispose your context and have no more entity references. Unless you are keeping (maybe not-so-obvious) references to the entities or the context somewhere, they should be disposed and collected automatically. That's why I recommended using a memory profiler. – Jcl May 30 '12 at 06:23
0

How are you checking the memory usage, if you are using Task Manager then it does not reflect the correct memory usage. Its better if you use some memory profiler for .Net, also see What Are Some Good .NET Profilers?. You can also try dotTrace to see which object is taking memory.

try disabling ContextOptions.LazyLoadingEnabled under your data context and see if it helps, but I still think using a memory profiler would give you a clear picture of what is going on with your applicaiton.

Community
  • 1
  • 1
Habib
  • 219,104
  • 29
  • 407
  • 436
0

Yes, EF will cache the entities you queried ,maybe this is a reason ,you can turn it off in dbcontext.config and c any effected.

kerryking
  • 104
  • 1
  • 6