Zorthgo
Zorthgo

Reputation: 2977

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! :)

Upvotes: 11

Views: 5654

Answers (2)

dvd94
dvd94

Reputation: 131

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.

Upvotes: 8

William Bosacker
William Bosacker

Reputation: 91

The server is caching the output, 100,000,000 * 4 characters ~ 411MB.

Upvotes: 0

Related Questions