首页 > 解决方案 > 在 .net core 项目中的 Linux 上运行时设置 GCServer = True

问题描述

如何在 .net 核心项目中将 GCServer 设置为 true?通常在 .net 框架项目中,我添加一个 App.Config xml 文件,将 GCServer 变量设置为 true ,但这在 Linux 上运行的 .net 核心项目中不起作用(生成并发布 App.Config 文件,但变量仍然没有t改变)

标签: c#.netlinux.net-coregarbage-collection

解决方案


添加<ServerGarbageCollection>True</ServerGarbageCollection>到您的 csproj 文件中。喜欢:

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

要确认在构建过程中设置正确,请检查目录<PROJECT>.runtimeconfig.json中的bin文件。它应该包含以下内容:

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

在某些情况下,GCServer 已经是默认值。您可以使用以下命令检查您的 msbuild 文件是否有默认值msbuild /pp

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

如果你有一个带有 的 csproj 文件Sdk="Microsoft.NET.Sdk.Web",那么它已经是默认的:

<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)" />

推荐阅读