How can users enable Background Server GC in a desktop app without changing app.config?

我的未来我决定 提交于 2020-01-17 13:41:21

问题


I look after a WPF desktop app which many users run on varying hardware. Multiple threads constantly churn through a large volume of data.

Turning on .NET Framework 4.5 Background server garbage collection has shown significant performance improvements, however this comes at the cost of more memory being used and different CPU usage patterns. This is a potential problem for users with less-powerful machines, so I want to give the users who'd benefit most an opt-in to having server GC enabled instead of forcing it upon everyone.

The only way I've found so far to enable this is in App.Config:

<configuration>
  <runtime>
    <gcServer enabled="true"/>
  </runtime>
</configuration>

However, most users do not have permissions to edit this as the app resides within Program Files and they're not local admins. My ideal scenario would be a friendly-worded option which could restart the app with server GC enabled and remember their choice.

So my question is: Does anyone know of any other way to apply this setting?

I know it's probably wishful thinking - but ideally something dynamic at runtime, or maybe a command-line argument or environment variable which could be set in the context of the process. There are hints of this existing in CoreCLR with the complus_gcServer env variable.

I've read about global system settings which can enable this on the machine but I don't want to risk impacting any other apps and causing other adverse affects, I only want this to apply to my app.

As a last resort I could deploy two copies of the EXE with different config files and a launcher to choose the correct version to run, this seems extreme though.

Thanks for reading, any ideas welcome!


回答1:


Here is an alternate suggestion. Leave server GC enabled as you have done. Then based on a user defined setting force GC to run if app memory goes above a certain threshold that you determine as the critical level.

Mind you if you follow this path you are effectively stating that you have better knowledge of when garbage collection should run that the CLR does. Most often than not I've found that the CLR does a better job on its own than when we interfere with it.

Code to check memory usage and run GC across either all generations or a specified generation

if (Math.Abs(DateTime.Now.Subtract(MemUsageLastCheckTime).TotalMinutes) > 5d)
{
    long UsedMemory;
    //UsedMemory = GC.GetTotalMemory(false); // Not as reliable
    UsedMemory = System.Diagnostics.Process.GetCurrentProcess().PagedMemorySize64;
    if (UsedMemory > 1073741824) // One GB in bytes 1 X 1024 X 1024 X 1024
    {
        GC.Collect(); // Collect all generations
        //GC.Collect(2,GCCollectionMode.Forced);; Or collect a specific generation and force it to run now
    }
    MemUsageLastCheckTime = DateTime.Now;
}


来源:https://stackoverflow.com/questions/40370575/how-can-users-enable-background-server-gc-in-a-desktop-app-without-changing-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!