Reducing .NET Core Memory Usage

Ray HuangOver the months of troubleshooting .NET Core memory issues, one of our customers kindly pointed out to us there is a simple solution you can apply, and that is to change the server garbage collection mode from server to workstation.  To do that, just change the “System.GC.Server” element in the project.json file in Visual Studio 2015 from “true” to “false“:

"runtimeOptions": {
  "configProperties": {
    "System.GC.Server": false
  }
}

And because the settings have moved to the ASPNETCore.csproj file for Visual Studio 2017, you need to change the “ServerGarbageCollecton” XML node from “true” to “false“:

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

This is because according to Mark Vincze:

the CPU count greatly affects the amount of memory .NET will use with Server GC

And since all of the DiscountASP.NET’s servers run on multiple processors, changing this value should reduce the amount of memory that your .NET Core application will use.  Many thanks to Mark Vincze and his tests to help the .NET Community out.

9 thoughts on “Reducing .NET Core Memory Usage

  1. This might sound obvious but this is a setting you are changing your IIS server?

    Do we need to make any changes on anything such as web.config file?

    In a nutshell it should mean that my web application is less likely to run out of memory and restart every so often?

    1. This isn’t a server change, it’s a method that you can implement in your own application to reduce memory usage for .NET Core apps.

  2. Hello,

    According to the documentation you pointed readers to, it’s best to set garbage collection to “true” for multiprocessor CPUs, and “false” for single processor CPUs. Have I misinterpreted that?

      1. You’re recommending we change it to false, despite telling us that your servers have multiprocessor CPUs. Isn’t that the opposite?

        I’m trying to understand this a little better, because I’m new to asp.net core memory usage optimization.

        Thank you!

  3. Hi Ben,

    Sorry, my fault. I misread your response. No, you want to set it to “false” or “off”. Having it “on” or set to true increases the memory usage.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.