Is there a straightforward function call to determine if Concurrent GC has been enabled in the runtime I'm running in? We have a heterogeneous environment and we need to log which mode is being used so we can identify which systems need to be modified.
I realise that I can investigate the exe.config and check it manually, I was just wondering if there is a property sitting somewhere that exposes this info without having to make a hack.
Sadly it doesn't look like there is a method baked in, if there was I would expect to see it here: System.Runtime.GCSettings.
.
Unless you have a good reason its best to leave concurrent garbage collection on (as its the default).
Generally the only things that care about this are unmanaged applications that are hosting the CLR themselves (such as SQL Server or IIS).
Is your application a good candidate for programmatically changing the gcConcurrent in the exe.config? If not, it would seem as you said, reading the value from the config is the less hackish way (IMHO).
Never the less, I did a bit of research and starting with .Net 4 you can use ETW to detect garbage collection ETW events:
Event tracing for Windows (ETW) is a tracing system that supplements the profiling and debugging support provided by the .NET Framework. Starting with the .NET Framework 4, garbage collection ETW events capture useful information for analyzing the managed heap from a statistical point of view. For example, the GCStart_V1 event, which is raised when a garbage collection is about to occur, provides the following information:
Update:
bool isServerGC = GCSettings.IsServerGC;
Console.WriteLine($"Server GC Enabled: {isServerGC}");
// if concurrent GC is enabled:
if (!isServerGC)
{
Console.WriteLine("Running in Workstation GC mode.");
// On Workstation GC, concurrent GC is generally enabled by default.
Console.WriteLine("Concurrent GC Enabled: Likely enabled.");
}
else
{
Console.WriteLine("Concurrent GC is not used in Server GC mode.");
}
Using the App.Config you can enable it or with an EnvVar COMPlus_gcConcurrent=1
:
<configuration>
<runtime>
<gcConcurrent enabled="true" />
</runtime>
</configuration>