2

Sorry for the vague topic title, but I don't know what's going on so I cannot be clearer right now. Basically, I've noticed the IIS app pool eating huge amounts of memory for an api that we have running.

I have a Web api method that returns a list of customers.

public async Task<IHttpActionResult> GetCustomerList()
{
    List<CustomerDTO> dtoCustomers = await customerService.GetCustomers();

    return CreatedAtRoute("DefaultApi", new { }, dtoCustomers);
}

And here is the GetCustomers method:

public async Task<List<CustomerDTO>> GetCustomers()
{
    List<CustomerDTO> lstCustomers = new List<CustomerDTO>();
    using (DBContext db = new DBContext())
    {
        var Customers = await db.Customers.Where(i => i.Deleted == false).OrderBy(i => i.CustomerName).ToListAsync();
        foreach (var Customer in Customers)
        {
            lstCustomers.Add(Mapper.Map<CustomerDTO>(Customer));
        }
    }
    return lstCustomers;
}

The AutoMapper mapping call just news up a instance of the Customer object and sets the values (just name and address) from the entity object.

Each call returns about 100K of data. On each call, the IIS worker process increases it's memory usage by ~50-100MB or more. Running the method several times sends the worker process memory skyrocketing. I did a test whereby I returned an empty list from the GetCustomers() call and the IIS memory usage still keeps going up but at a much slower rate (~80K per call).

So the IIS worker just keeps eating memory too quickly and I'm surprised it's not being garbage collected. I could set a memory limit on the app pool but it would just be recycling over and over again throughout the day.

Does anybody see anything particularly stupid with what I'm doing that is causing this memory consumption? Any thoughts would be appreciated.

K. Meke
  • 433
  • 2
  • 14
  • This could be a really complex problem, and this question is probably too broad. Anyone would need access to all code and configuration to truly help you. Otherwise we can just give advice. Use a memory profiler to analyze what objects are not being collected and why. What exactly is the problem here? Is your server running low on memory? Fighting with .NET about memory can be an uphill battle. – Nick Apr 09 '15 at 16:40
  • So, I was reading [this question](http://stackoverflow.com/questions/24148921/c-sharp-webapi-garbage-collection) and I took the advice re: using ANTS Profiler. The funny thing is that while profiling the api (profiler pipes the url through port 8013) the memory usage is stable even with ~100 calls and most of the mem usage is unused memory allocated to .NET. When I do api call without profiler (no port 8013) app pool memory spikes as described. – K. Meke Apr 09 '15 at 16:55
  • @Nick, you wrote your comment when I was writing mine! I'm now wondering if this IS a problem, although the memory usage of the app pool appears to be real enough. Examining the allocated objects in the memory snapshots, they are mostly Entity Framework-related. Also, to answer your question, server is not running low on memory - just happened to notice that an unexpectedly large amount of memory was being used by various app pools and started to investigate. I'm now wondering if this is actually a problem or not.... – K. Meke Apr 09 '15 at 17:00
  • Using Entity Framework can introduce a lot of overhead. If it becomes a problem, you can take steps to gain control over EF's caching behaviors. – Nick Apr 09 '15 at 17:25
  • The Question [WebApi 2 - RAM usage](http://stackoverflow.com/questions/22662399/) was closed but some comments could be usefull (compile as 32bit). The question [High memory usage by ASP.NET applications](http://stackoverflow.com/questions/28301379) seems to have a similar problem; advice was to close and dispose db-connections, test and remove third party libraries. – surfmuggle Oct 21 '16 at 15:15

0 Answers0