gcServer 设置未从非托管 exe 传递到托管 dll

问题描述

我有一个由非托管代码调用的 .net 库(框架 v4.6.1),并想配置其垃圾回收。我已尝试使用下面的 app.exe.config,但 GCSettings.IsServerGC 返回 false,因此无法正常工作。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <runtime>
    <gcServer enabled="true" />
    <gcallowverylargeobjects enabled="true" />
  </runtime>
</configuration>

运行 procmon,我可以验证这确实是文件被打开和读取。可能出什么问题了?我已经尝试将 COMPlus_gcServer 环境变量设置为 1,但也无法启用服务器模式。

解决方法

显然,这个 unmanaged.exe.config 设置被 .Net 4.x 弄乱了,但如果托管 dll 被告知在应用程序启动后足够快地进行 CLR 调用,则可以在 managed.dll.config 中找到它。可以在此处找到此解决方法的详细信息:

C# COM DLL with gcServer = true and gcAllowVeryLargeObjects = true