Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Cloud Service running Web Role (web application) memory issue (Gen2)

I have a question regarding high memory usage of Web Role running MVC application, with Simple Injector as DI, Entity Framework 6 for DAL. Application is running on Azure Cloud Service as Web Role with 2 x Standard A2 Instances (2 Cores, 3.5 GB RAM) and is also running CachingService (Co-located Role) with 20% memory usage configured.

Problem is that when instance is started or rebooted the memory usage of w3wp.exe service is only around 500-600 MB (with all other apps memory usage is around 50%), but even if there are no requests coming in it starts and continues growing until around 1.7GB and stops (with all other apps memory usage is around 90%). But what I noticed is that memory drops sometimes randomly and of course after reboot or republishing.

After monitoring memory heaps I noticed that it is Gen2 Heap that grows and stays large and after debugging locally with ANTS Memory Profiler I saw that largest amount of Gen2 is taken by Entity Framework objects with class name "TypeUsage" and "MetadataProperty" objects ("System.Data.Entity.Core.Metadata.Edm" namespace).

Now my question are:

  • is this a memory leak in our code and how can I solve it if that is the case (I checked and already tried to dispose DbContext that is created every request)?
  • is this a memory leak in EF, if that is the case what can I do about this, maybe another DAL framework?
  • is this a normal behavior and I should leave it as it is?
like image 249
migontech Avatar asked Nov 09 '22 20:11

migontech


1 Answers

There is a very low chance that this is a memory leak in EF and this is not OK and you shouldn't leave it like this. Your code leaks memory.

The best way to find the leak is to use a memory profiler (ANTS is a good option, I used dotMemory). The profiler will show you the leaked objects and it should also show you two other important things:

  • The stack trace of the location in code where the object was created
  • The object tree which keeps reference to your leaked object and doesn't allow it to be collected.

These should help you understand how the objects were created and why they weren't GC'ed.

You mentioned that most of the memory is in Gen2. That means that your leaked objects are referenced by something "long lived". This could be a static variable, ASP.Net Application, or something similar.

The random drop of memory may occur when IIS recycles your application. By default that happens every 29 hours, but IIS may be configured differently or may decide to recycle your application for some other purpose.

like image 155
Alon Catz Avatar answered Nov 15 '22 05:11

Alon Catz