2

I am running into some problems where huge objects occupy memory and the reference is not being released. I used .Net Memory Profiler to find out the root object and it references to Entity Framework class.

Is there a way i can disable the caching of queried objects in Entity framework without chanign the code? Something in the config file may be?

Asdfg
  • 9,239
  • 21
  • 82
  • 151
  • 3
    [Why you should not use static context.](http://stackoverflow.com/questions/3653009/entity-framework-and-connection-pooling/3653392#3653392) In case of server application or multithreaded application change **should not** with **must not**. – Ladislav Mrnka Jul 01 '11 at 14:21

1 Answers1

3

You should have using statements to help dispose you ObjectContext. EF keeps an object graph of the queried objects. I think you could use objectContext.Detach(Entity); to detach your entities from the ObjectContext.

StriplingWarrior
  • 135,113
  • 24
  • 223
  • 283
Jethro
  • 5,740
  • 2
  • 20
  • 23
  • +1: The Entity Framework contexts are `IDisposable`, and you should expect that they will consume a lot of resources if they are not disposed of quickly. – StriplingWarrior Jul 01 '11 at 14:10
  • 2
    what if i say my EF object context is in a Static class? Does this raise the red flag? – Asdfg Jul 01 '11 at 14:10
  • 1
    I would think so, because every object you load will stay loaded for the life of the application. IMO Your context should be created, as little data pulled into it as needed, and then thrown away when finished. – The Evil Greebo Jul 01 '11 at 14:15
  • @Asdfg, Yes a big red flag. Please see the following links for more detailed exp. http://stackoverflow.com/questions/1990876/is-it-good-to-use-a-static-ef-object-context-in-an-mvc-application-for-better-per , http://stackoverflow.com/questions/1785119/is-it-safe-to-store-an-objectcontext-in-a-thread-static-variable-in-asp-net – Jethro Jul 01 '11 at 14:16