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.
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! :)
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.
. 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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With