Robert J. Robert J. - 16 days ago 8
ASP.NET (C#) Question

MVC application memory management

I would like to get more information regarding memory management in the ASP.NET MVC (5) apps.

For the sake of simplicity, let's say that we have a simple page with angular controller, which only makes a

http.get()
call towards my
api controller
to retrieve 70 000 items of
IEnumerable<CustomItem>
and afterwards outputs the result to the page (paging is implemented).

When looking at my diagnostic tools (memory profiler) I can see memory rising from 50 MB (starting point) up to 300 MB (once all the data is retrieved).

Here is the thing I don't understand:

Why when I open the same page with different browser at the same time (trying to simulate that more users are visiting the page at the same time), the memory consumption for second user does not rise by another 250 MB but it remains the same even though the data has been loaded again?

I was under impression, that
ApiController
is not shared among users but new instance is being created for every user. If I am right and it's really the case, how come that when 70 000 more items arrived the memory consumption didn't change?

Any articles or explanations would be highly appreciated.

Addition 1:

My api controller is rerieving data from
SharePoint
and therefore I have to pass all 70 000 records at once to my angular component, which afterwards implements pagination. In other words, my components loads only once all the data (70 000 records) and afterwards displays only portion at the time (otherwise if I used
data-ng-repeat
on 70 000 items my browser used to freeze).

I can surely show my
ApiController
code if necessary, but really its just 3 lines of code which return
IEnumerable<CustomItem>
which only holds three string properties. This is how it looks like:

public class SharepointFile
{
public string FileType { get; set; }
public string FileName { get; set; }
public string FilePath { get; set; }
}

Answer

The memory rise you see is actual memory used by the process. But .NET may not allocate more memory if memory is free from previously executed request, although all objects will be garbage collected and will be newly allocated, but .NET may not require more memory. Usually processes do not release memory unless are told by OS to do so. They keep on using the memory space.

Unnecessary releasing the memory after each request will require lot of CPU usage and it will create lot of fragmentation in memory at operating system level.

There is always difference between total size of active .NET objects and memory reserved for them.

So for your second request, all 70k objects are not shared, but they are new ones, but process already has empty space to store them, so it will not request more memory from OS.

Comments