none
How do we tell the .NET 4.0 CLR to use server garbage collection, when hosting the CLR ourselves? RRS feed

  • Question

  • We have a server app product.   This runs unmanaged code under COM+ for historical reasons.   There are also parts of the system which are managed code.

    We host the CLR ourselves, using unmanaged hosting interfaces.   The reason for doing so is so that the app handles exceptions such as stack overflow gracefully, rather than following the default behaviour which is for the process to crash.

    When we originally started hosting the.NET 2.0 CLR, we used the CorBindToRuntimeEx function.    Since moving up to .NET 4.0 however this function has been deprecated in favour of the sequence described in the MSDN article Loading the Common Language Runtime into a Process.

    The reason I mention this is that the 2nd argument for CorBindToRuntimeEx specfies the GC flavor to use – ie whether to use workstation garbage collection, or server garbage collection.   Previously we supplied “svr” as the 2<sup>nd</sup> argument, indicating that we want to use server garbage collection.

    Given that CorBindToRuntimeEx  is now deprecated, it is not clear to us how to indicate a preference for using server garbage collection when hosting the .NET 4.0 CLR.   Furthermore we can see that the default workstation garbage collection is in use, both by checking the GCSettings.IsServerGC property, and also by examining a full memory dump of the executing process.

    Online there are suggestions to specify the GC flavor in the .config file like this:

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

    However, given that we previously had to specify the GC flavour in the call to CorBindToRuntimeEx, I would assume we still need to do so explicitly.   This is also suggested by a line in the MSDN article Fundamentals of Garbage Collection“You can also specify server garbage collection with unmanaged hosting interfaces”.

    As things stand, the GCSettings.IsServerGC property always returns false – even if we make the above alteration to the .config file.   And note that this is seen when running on a quad-core machine.

    Suggestions as to how we tell our app to use server garbage collection gratefully received.

    Thanks in advance.

    Ed Barrett

    Wednesday, January 8, 2014 12:06 PM

Answers

All replies