Entity framework uses a lot of memory

后端 未结 4 2294
再見小時候
再見小時候 2021-01-04 05:44

Here is a image from the ANTS memory profiler. It seens that there are a lot of objects hold in memory. How can I find what I am doing wrong?

相关标签:
4条回答
  • 2021-01-04 05:56

    Do you clear the ObjectContext once in a while. If you keep an ObjectContext alive for a long time this will consume memory related to the size of the EntityDataModel and the number of Entities loaded into this ObjectContext.

    0 讨论(0)
  • 2021-01-04 05:59

    I had the same problem in a class which uses dependency injection, so the using() option was not an alternative. My solution was to add DbContextOptions<Context> to the constructor and as a private field to the class. Then, you can call

    _db.Dispose();
    _db = new BlockExplorerContext(_dBContextOptions);
    

    at appropriate times. This fixed my problem where I was running out of RAM and the application was killed by the OS.

    0 讨论(0)
  • 2021-01-04 06:16

    Couple of things come to my mind:

    • You aren't probably Disposing the ObjectContext. Make sure all your database codes are within using(var context = CreateObjectContext()) block
    • You have an N-tier architecture and you are passing entities from the data access layer to upper layer without Detaching the entities from ObjectContext. You need to call ObjectContext.Detach(...)
    • You are most likely returning a full collection of entities, instead of returning a single enity for single Get operations. For ex, you have queries like from customer in context.Customers select customer instead of doing from customer in context.Customers select customer.FirstOrDefault()

    I have had hard time making Entity Framework to work in an N-tier application. It's just not suitable for using in N-tier apps as is. Only EF 4.0 is. You can read about all my adventure in making EF 3 work in an N-tier app.

    http://www.codeproject.com/KB/linq/ef.aspx

    Does this answer your question?

    0 讨论(0)
  • 2021-01-04 06:18

    I have a hunch you don't dispose the context.
    I suggest disposing the context whenever you done interacting with database.

    Use using statement whenever you create the context.

    [Edit]

    As far as I can see, you cache and don't dispose your EFUnitOfWork object. It is disposable, which is correct, but I don't see when disposable is called. Seems like you hold a reference to the context for all application run time.
    Moreover, you create and hold one context per thread, which will make it even worse.

    I can't tell you for sure where you should put Dispose or using, as I don't know the usages.
    You could put it probably to your Commit method, but I don't know if the Commit called only once during database interaction session.

    Also, your design might be overcomplicated.

    If I were you, I would:

    • Find the way to dispose the context using current code, as a short-term solution
    • Simplify the design, as the long-term solution

    If I had time I would do long-term solution right away.
    But again, I can't tell if the complexity of your design is justified, as I don't know how big your application is and what it does and what the requirements are.

    0 讨论(0)
提交回复
热议问题