Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net Core not Collecting Garbage

I can't seem to understand why Asp.net core is not collecting garbage. Last week I let a web service run for a few of days, and my memory usage reached 20GB. GC doesn't seem to be working. So to test this I wrote a very simple web method that return a large collection of strings. The application started off using only 124MB, but with each time I called the web method, the memory usage kept getting higher and higher until it reached 411MB. It would have gone higher if I had kept calling the web method. But I decided to stop testing.

enter image description here

Does anyone know why the GC is not working? As you can see from the performance monitor the GC was called (the yellow marker on the graph). But it did not collect the garbage from memory. I would think that the GC would eagerly collect anything that didn't have a reference to it.

Any help will be GREATLY appreciated. Thanks! :)

like image 707
Zorthgo Avatar asked Mar 21 '17 04:03

Zorthgo


People also ask

Who does garbage collection in .NET core?

In this chapter, we will cover the concept of Garbage collection which is one of most important features of the . NET managed code platform. The garbage collector (GC) manages the allocation and release of memory.

Does .NET have garbage collection?

. NET's garbage collector manages the allocation and release of memory for your application. Each time you create a new object, the common language runtime allocates memory for the object from the managed heap.

How do you make sure that the garbage collector is done running when you call GC collect ()?

To have the garbage collector consider all objects regardless of their generation, use the version of this method that takes no parameters. To have the garbage collector reclaim objects based on a GCCollectionMode setting, use the GC. Collect(Int32, GCCollectionMode) method overload.

Does C# support garbage collection?

I also know that C# does it's own Garbage Collection (ie. It determines when an instanciated class is no longer in use and reclaims the memory). The C# language does not do so; the CLR does so.


1 Answers

I've ran into the same problem and after a long day found a solution in one of the github issues registered for high memory consumption.

Actually, this is expected behaviour (in a multi-core high memory machine) and called "Server" garbage collection mode. This mode is optimized for server load and runs GC only when it's really needed (when it starts to lack memory in a machine).

The solution is to change GC mode to "Workstation" mode. You can do this by adding a setting to your .csproj

<PropertyGroup>
  <ServerGarbageCollection>false</ServerGarbageCollection>
</PropertyGroup>

Workstation mode is meant to use less memory but run GC more often.

It is well-documented by Sebastien Ros here: https://github.com/sebastienros/memoryleak

On a typical web server environment the CPU resource is more critical than memory, hence using the Server GC is better suited.

like image 146
dvd94 Avatar answered Sep 29 '22 14:09

dvd94