Entity framework uses a lot of memory

后端 未结 4 2291
再見小時候
再見小時候 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 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?

提交回复
热议问题