Big Generator 0 Heap in .NET, wasting memory?

I have an ASP.NET/C# web application that uses a lot of memory.

ANTI Memory Profiler and PerfMon show that my Gen 0 heap is growing rapidly to around 1 GB during Application_Start. I read here that the PerfMon counter for the Gen 0 heap actually shows the "budget" for Gen 0, and not the size (which I accept to mean not all that memory is part of a private workflow?). However, the ANTS profiler shows about 700 MB of “unused memory allocated for .NET,” and this seems to be part of a private workflow (as specified in taskmgr). I assume that this large amount of unused memory is associated with a large heap of Gen 0.

What happens in Application_Start when this happens is that in the while loop I read about a million rows from SqlDataReader. They are used to populate a large cache for later use. Given this, the obvious culprit of a large amount of unused memory was the large fragmentation of the heap of objects, but I do not think so, since I pre-allocate more than is necessary for my large cache object. To be sure, I even tried to comment on the part of the loop that actually adds to my cache object; it had no significance in the amount of allocated unused memory.

As a test, I often tried to force garbage collection of generator 0 during the cycle (against all the recommendations, I know), and this led to the gen0 heap size remaining below 128 MB, and also led to only a few MB of unused free memory. But he also exceeded my processor and forced Application_Start to go too long.

My questions:

1) What could cause the reported Gen 0 heap size to be so large?

2) Is this a problem? In particular, could this be the reason for placing a large amount of unused space for .NET?

3) If so, what should I do to fix this? If I cannot stop the process from using so much memory during Application_Start, I would like to at least make it drop memory when the application shuts down.

+6
source share
1 answer

Gen 0 contains the “youngest, most recently allocated objects” and is separate and distinct from LOH. It looks like you are highlighting tons and tons of small objects (all errors associated with these cache entries by sound) that are not used (shown by a frequent GC, keeping the size down), but not cleared in a timely manner because the GC has not yet found it necessary . GC just did not see the need for cleaning. How much RAM does the machine have? I assume that you are not paging.

I understand that .NET can return pieces of an unused heap in the OS when the GC considers that it is no longer needed (because this heap space has not been used for a long time, for example). Have you watched the app for a long period of time to make sure this is happening? If you are not swapping, I do not think this is a problem.

+4
source

Source: https://habr.com/ru/post/905349/


All Articles