Scott Hanselman

Using the Server (rather than Workstation) Garbage Collector with the .NET Framework (CLR)

July 15, 2004 Comment on this post [2] Posted in ASP.NET | XML | Tools
Sponsored By

We run a big .NET Application Server, often on multi-proc machines, so we care about performance and, consequently, garbage collection.  I've collected a few resources around the two kinds of Garbage Collectors available in .NET, the Workstation GC and the Server GC.

From the MSDN Help:

Two different Garbage Collectors are available for the CLR: a Workstation GC and a Server GC. Console and Windows Forms applications host the Workstation GC, and ASP.NET hosts the Server GC. The Server GC is optimized for throughput and multi-processor scalability. The server GC pauses all threads running managed code for the entire duration of a collection, including both the Mark and Sweep Phases, and GC happens in parallel on all CPU's available to the process on dedicated high-priority CPU-affinitized threads. If threads are running native code during a GC then those threads are paused only when the native call returns. If you are building a server application that is going to run on multiprocessor machines then it is highly recommended that you use the Server GC. If your application in not hosted by ASP.NET, then you are going to have to write a native application that explicitly hosts the CLR.

HINT:   If you are building scalable server applications, host the Server GC. See Implement a Custom Common Language Runtime Host for Your Managed App.
  • Hosting the CLR in a generic service and Hosting the Server GC: "To load the svr GC, we created a generic Windows service that loads the svr GC, creates an AppDomain, and runs our application."
     CComPtr spRuntimeHost;
     HRESULT hr = CorBindToRuntimeEx(NULL, 
       //Retrieve latest version by default
      L"svr",
     
       //Request a Server (svr) or WorkStation (wks) build of the CLR
      STARTUP_LOADER_OPTIMIZATION_SINGLE_DOMAIN, 
      CLSID_CorRuntimeHost,
      IID_ICorRuntimeHost,
      (void**)&spRuntimeHost);
  • Using the Server Garbage Collector:  
    1. Create the system environment variable COMPLUS_BUILDFLAVOR to SVR
    2. SET COMPLUS_BUILDFLAVOR = SVR
    3. Generate this key in the windows registry: HKLM/Software/Microsoft/COMPlus with a single string value BuildFlavor with value "svr"
    SDH Note: This is fully unsupported my Microsoft.
  • Improvements in .NET 1.0 SP3 and .NET 1.1 SP1 and How to enable CLR Server GC?
     <Configuration>
        <runtime>
            <gcServer enabled=“true“ />
        </runtime>
    </Configuration> 

From Chris Kinsman: Here’s a way to do it that’s not a hack – right-click on My Computer in .NET Configuration in Administrative Tools. UPDATE: In fact, this is NOT so, details on this setting at OdeToCode.com

 

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
July 16, 2004 0:38
You can also turn it on for the whole server in .NET Framework Config by right clicking on the machine and picking run in the foreground for server applications.
July 16, 2004 7:17
Actually, Scott, the MMC setting Chris suggested doesn't select between workstation and server GC. See http://odetocode.com/Blogs/scott/archive/2004/07/15/323.aspx .

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.