Skip to content
Advertisement

Setting GCServer = True when running on Linux in .net core project

How do set GCServer to true in a .net core project? Usually in .net framework projects I add an App.Config xml file that sets GCServer variable to true but this does not work in a .net core project running on Linux (the App.Config file is generated and publish but the variable still doesn’t change)

Advertisement

Answer

Add <ServerGarbageCollection>True</ServerGarbageCollection> to your csproj file. Like:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    ...
    <ServerGarbageCollection>true</ServerGarbageCollection>
    ...
  </PropertyGroup>
</Project>

To confirm that it’s being correctly set during build, check your <PROJECT>.runtimeconfig.json file in the bin directory. It should contain something like:

"configProperties": {
  "System.GC.Server": true
}

In some cases, the GCServer is already the default. You can check if there’s a default value your msbuild file by using msbuild /pp:

$ dotnet msbuild /pp | grep -i ServerGarbage
<ServerGarbageCollection>true</ServerGarbageCollection>
<RuntimeHostConfigurationOption Include="System.GC.Server" Condition="'$(ServerGarbageCollection)' != ''" Value="$(ServerGarbageCollection)" />

If you have a csproj file with Sdk="Microsoft.NET.Sdk.Web", then it’s already the default:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>
</Project>

$ dotnet msbuild /pp | grep -i ServerGarbage
<ServerGarbageCollection>true</ServerGarbageCollection>
<RuntimeHostConfigurationOption Include="System.GC.Server" Condition="'$(ServerGarbageCollection)' != ''" Value="$(ServerGarbageCollection)" />
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement