Huge memory Consumption (MVC 5 + EntityFramework 6.2.0 )

autofac c# entity-framework entity-framework-6


I am having some concerns about my project using a lot of memory. I have configured my MVC 5 project with Entity Framework. I also use Autofac.

I locate my repository classes and service classes (which are in the same project) by doing:

            .Where(t => t.Name.EndsWith("Repository"))

(And one similar for the services in that project. )

Now I can see when debugging that I initialize my _dbContext if it is not already and at the end I can see the dispose is called successfully. Upon launching the page, the memory usage is around 460 meg, and if I keep refreshing it goes slightly up and reaches to 500 meg and when I stop 10~15 mins later it drops back to roughly 460 meg.

Now I wanted to test something so I created a DBSet using the following model:

    public class Email
       public int ID { get; set; }
       public string Subject { get; set; }
       *[StringLength(32, ErrorMessage = "Must be under 32 characters")]*
       public string From { get; set; }
       public DateTime Sent { get; set; }
       public long Size { get; set; }
       public Boolean HasAttachment { get; set; }

Two things to consider:

  • I loaded the DBSet with 200,000 records of the above Email
  • Each record's Subject field is set to an string that is 2500 characters long

Now when I add a call to my method to just retrieve the data by:

var emails = _dbEmailService.GetAllEmails().AsQueryable();

My memory usage shoots up to 2Gig...then every time I refresh it adds up another 1 Gig.

That is really bad isn't it? can I get some help please to figure out why? I have spent 1.5 days on this and very frustrated. Thanks.

6/7/2018 11:18:21 AM

Accepted Answer

Thanks for everyone's help. Hany your question made me go back and re-evaluate my paging approach. Initially I was getting the same problem with the paging, and that is why I started trying to retrieve all the records to see where the bottleneck is. But now after looking closer, I noticed that the paging wasn't really working because my Lambda was retrieving all the records, not just a IQueryable. Then there was a second problem where the request to DB was taking 3 seconds. This was due to lack of index on the ID column. Once I added it, everything started working as it should, with low memory footprint and very fast. Its amazing a few hours break from the screen can do. :)

6/8/2018 8:25:55 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow