C# tutorials > Memory Management and Garbage Collection > .NET Memory Management > What are the different garbage collection modes?
What are the different garbage collection modes?
Understanding Garbage Collection Modes
Workstation Garbage Collection
Server Garbage Collection
Concurrent vs. Non-Concurrent Garbage Collection
Choosing the Right Mode
Configuring Garbage Collection Mode (Example - app.config)
gcServer
setting in the app.config
or web.config
file. Setting enabled="true"
enables Server GC, while setting it to enabled="false"
enables Workstation GC. Remove the comment for the line of the GC type you wish to use. Note: This configuration is typically done during deployment and is not changed dynamically during runtime.
<!-- app.config or web.config -->
<configuration>
<runtime>
<gcServer enabled="true"/> <!-- Enable Server GC -->
<!-- gcServer enabled="false"/ --> <!-- Enable Workstation GC -->
</runtime>
</configuration>
Configuring Garbage Collection Programmatically
System.Runtime.GCSettings.IsServerGC
property. Note that this property can only be set before any garbage collection has occurred in the application domain. Trying to set it after GC has started will throw an exception. This approach requires .NET Framework 4.5.1 or later. The `Console.WriteLine` line is for verifying which mode is active. Important: Programmatic configuration should be done very early in your application's startup process (e.g., within the `Main` method).
// Programmatically configure GC (Requires .NET 4.5.1 or later)
System.Runtime.GCSettings.IsServerGC = true; // Enable Server GC
// Check current GC mode
bool isServerGC = System.Runtime.GCSettings.IsServerGC;
Console.WriteLine($"Server GC is enabled: {isServerGC}");
Concepts Behind the Snippet
Real-Life Use Case Section
Desktop Application (Workstation GC): A desktop application like a photo editor relies heavily on responsiveness. Users expect immediate feedback when interacting with the application. Workstation GC ensures that garbage collection pauses are minimal, preventing the UI from freezing or becoming unresponsive, even when dealing with large image files.
Best Practices
Test Thoroughly: Always test your application with different GC modes under realistic workloads to identify potential performance bottlenecks.
Monitor Performance: Use performance monitoring tools to track GC activity and identify opportunities for optimization.
Avoid Excessive Object Creation: Minimize the creation of temporary objects, as this increases the frequency of garbage collection.
Dispose of Resources Properly: Implement the IDisposable
interface and use using
statements to ensure that resources are released promptly.
Interview Tip
When to Use Them
Server GC: Use when throughput is the priority, and the application can tolerate occasional pauses. Examples: web servers, backend services, batch processing applications.
Memory Footprint
Alternatives
Pros (Server GC)
Cons (Server GC)
Pros (Workstation GC)
Cons (Workstation GC)
FAQ
-
How do I determine which GC mode is currently active?
You can use theSystem.Runtime.GCSettings.IsServerGC
property to check the currently active GC mode programmatically. -
Can I change the GC mode dynamically at runtime?
No, you cannot change the GC mode dynamically after the application domain has been initialized and garbage collection has started. You must configure it before any garbage collection occurs. -
What is the default GC mode for ASP.NET applications?
The default GC mode for ASP.NET applications is Server GC. -
Does the number of processors affect the choice of GC mode?
Yes, Server GC is more effective on multi-processor systems because it utilizes multiple threads for garbage collection. Workstation GC is generally preferred on single-processor systems or when responsiveness is more critical than throughput.