title: Monitoring Garbage Collector in C#
body: <p>I have a WPF application that is experiencing a lot of performance issues. The worst of them is that sometimes the application just freezes for a few seconds before running again.</p>
  
  <p>I am currently debugging the application to see what this freeze might be related to, and I believe that one of things that may be causing it is the Garbage Collector. Since my application is running in a very limited environment, I believe that the Garbage Collector can be using all of the machine's resources when it is ran and leaving none to our application.</p>
  
  <p>To check this hypotheses I found these articles: <a href="http://msdn.microsoft.com/en-us/library/cc713687.aspx" rel="noreferrer">Garbage Collection Notifications</a> and <a href="http://www.abhisheksur.com/2010/08/garbage-collection-notifications-in-net.html" rel="noreferrer">Garbage Collection Notifications in .NET 4.0</a>, that explain how my application can be notified when the Garbage Collector will begin running and when it is finished.</p>
  
  <p>So, based on those articles I created the class below to get the notifications:</p>
  
  <pre><code>public sealed class GCMonitor
  {
      private static volatile GCMonitor instance;
      private static object syncRoot = new object();
  
      private Thread gcMonitorThread;
      private ThreadStart gcMonitorThreadStart;
  
      private bool isRunning;
  
      public static GCMonitor GetInstance()
      {
          if (instance == null)
          {
              lock (syncRoot)
              {
                  instance = new GCMonitor();
              }
          }
  
          return instance;
      }
  
      private GCMonitor()
      {
          isRunning = false;
          gcMonitorThreadStart = new ThreadStart(DoGCMonitoring);
          gcMonitorThread = new Thread(gcMonitorThreadStart);
      }
  
      public void StartGCMonitoring()
      {
          if (!isRunning)
          {
              gcMonitorThread.Start();
              isRunning = true;
              AllocationTest();
          }
      }
  
      private void DoGCMonitoring()
      {
          long beforeGC = 0;
          long afterGC = 0;
  
          try
          {
  
              while (true)
              {
                  // Check for a notification of an approaching collection.
                  GCNotificationStatus s = GC.WaitForFullGCApproach(10000);
                  if (s == GCNotificationStatus.Succeeded)
                  {
                      //Call event
                      beforeGC = GC.GetTotalMemory(false);
                      LogHelper.Log.InfoFormat("===&gt; GC &lt;=== " + Environment.NewLine + "GC is about to begin. Memory before GC: %d", beforeGC);
                      GC.Collect();
  
                  }
                  else if (s == GCNotificationStatus.Canceled)
                  {
                      LogHelper.Log.Info("===&gt; GC &lt;=== " + Environment.NewLine + "GC about to begin event was cancelled");
                  }
                  else if (s == GCNotificationStatus.Timeout)
                  {
                      LogHelper.Log.Info("===&gt; GC &lt;=== " + Environment.NewLine + "GC about to begin event was timeout");
                  }
                  else if (s == GCNotificationStatus.NotApplicable)
                  {
                      LogHelper.Log.Info("===&gt; GC &lt;=== " + Environment.NewLine + "GC about to begin event was not applicable");
                  }
                  else if (s == GCNotificationStatus.Failed)
                  {
                      LogHelper.Log.Info("===&gt; GC &lt;=== " + Environment.NewLine + "GC about to begin event failed");
                  }
  
                  // Check for a notification of a completed collection.
                  s = GC.WaitForFullGCComplete(10000);
                  if (s == GCNotificationStatus.Succeeded)
                  {
                      //Call event
                      afterGC = GC.GetTotalMemory(false);
                      LogHelper.Log.InfoFormat("===&gt; GC &lt;=== " + Environment.NewLine + "GC has ended. Memory after GC: %d", afterGC);
  
                      long diff = beforeGC - afterGC;
  
                      if (diff &gt; 0)
                      {
                          LogHelper.Log.InfoFormat("===&gt; GC &lt;=== " + Environment.NewLine + "Collected memory: %d", diff);
                      }
  
                  }
                  else if (s == GCNotificationStatus.Canceled)
                  {
                      LogHelper.Log.Info("===&gt; GC &lt;=== " + Environment.NewLine + "GC finished event was cancelled");
                  }
                  else if (s == GCNotificationStatus.Timeout)
                  {
                      LogHelper.Log.Info("===&gt; GC &lt;=== " + Environment.NewLine + "GC finished event was timeout");
                  }
                  else if (s == GCNotificationStatus.NotApplicable)
                  {
                      LogHelper.Log.Info("===&gt; GC &lt;=== " + Environment.NewLine + "GC finished event was not applicable");
                  }
                  else if (s == GCNotificationStatus.Failed)
                  {
                      LogHelper.Log.Info("===&gt; GC &lt;=== " + Environment.NewLine + "GC finished event failed");
                  }
  
                  Thread.Sleep(1500);
              }
          }
          catch (Exception e)
          {
              LogHelper.Log.Error("  ********************   Garbage Collector Error  ************************ ");
              LogHelper.LogAllErrorExceptions(e);
              LogHelper.Log.Error("  -------------------   Garbage Collector Error  --------------------- ");
          }
      }
  
      private void AllocationTest()
      {
          // Start a thread using WaitForFullGCProc.
          Thread stress = new Thread(() =&gt;
          {
              while (true)
              {
                  List&lt;char[]&gt; lst = new List&lt;char[]&gt;();
  
                  try
                  {
                      for (int i = 0; i &lt;= 30; i++)
                      {
                          char[] bbb = new char[900000]; // creates a block of 1000 characters
                          lst.Add(bbb);                // Adding to list ensures that the object doesnt gets out of scope
                      }
  
                      Thread.Sleep(1000);
                  }
                  catch (Exception ex)
                  {
                      LogHelper.Log.Error("  ********************   Garbage Collector Error  ************************ ");
                      LogHelper.LogAllErrorExceptions(e);
                      LogHelper.Log.Error("  -------------------   Garbage Collector Error  --------------------- ");
                  }
              }
  
  
          });
          stress.Start();
      }
  }
  </code></pre>
  
  <p>And I've added the gcConcurrent option to my app.config file (below):</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;configuration&gt;
    &lt;configSections&gt;
      &lt;section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net-net-2.0"/&gt;
    &lt;/configSections&gt;
  
    &lt;runtime&gt;
      &lt;gcConcurrent enabled="false" /&gt;
    &lt;/runtime&gt;
  
    &lt;log4net&gt;
      &lt;appender name="Root.ALL" type="log4net.Appender.RollingFileAppender"&gt;
        &lt;param name="File" value="../Logs/Root.All.log"/&gt;
        &lt;param name="AppendToFile" value="true"/&gt;
        &lt;param name="MaxSizeRollBackups" value="10"/&gt;
        &lt;param name="MaximumFileSize" value="8388608"/&gt;
        &lt;param name="RollingStyle" value="Size"/&gt;
        &lt;param name="StaticLogFileName" value="true"/&gt;
        &lt;layout type="log4net.Layout.PatternLayout"&gt;
        &lt;param name="ConversionPattern" value="%date [%thread] %-5level - %message%newline"/&gt;
        &lt;/layout&gt;
      &lt;/appender&gt;
      &lt;root&gt;
        &lt;level value="ALL"/&gt;
        &lt;appender-ref ref="Root.ALL"/&gt;
      &lt;/root&gt;
    &lt;/log4net&gt;
  
    &lt;appSettings&gt;
      &lt;add key="setting1" value="1"/&gt;
      &lt;add key="setting2" value="2"/&gt;
    &lt;/appSettings&gt;
    &lt;startup&gt;
      &lt;supportedRuntime version="v2.0.50727"/&gt;
    &lt;/startup&gt;
  
  &lt;/configuration&gt;
  </code></pre>
  
  <p>However, whenever the application is executed, it seems as if no notification is sent that the Garbage Collector will run. I've put breakpoints in the DoGCMonitoring and it appears that the conditions (s == GCNotificationStatus.Succeeded) and (s == GCNotificationStatus.Succeeded) are never satisfied, therefore the contents of those ifs statements are never executed.</p>
  
  <p>What am I doing wrong?</p>
  
  <p>Note: I am using C# with WPF and the .NET Framework 3.5.</p>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p>Updated my GCMonitor test with the AllocationTest method. This method is for testing purposes only. I just wanted to make sure that enough memory was being allocated to force the Garbage Collector to run.</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>Updated the DoGCMonitoring method, with new checks on the return of the methods WaitForFullGCApproach and WaitForFullGCComplete. From what I've seen so far my application is going directly to the (s == GCNotificationStatus.NotApplicable) condition. So I think that I have some misconfiguration somewhere that is stopping me from getting the desired results.</p>
  
  <p>The documentation for the GCNotificationStatus enum can be found <a href="http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&amp;l=EN-US&amp;k=k%28SYSTEM.GCNOTIFICATIONSTATUS%29;k%28GCNOTIFICATIONSTATUS%29;k%28TargetFrameworkMoniker-%22.NETFRAMEWORK,VERSION=V3.5%22%29;k%28DevLang-CSHARP%29&amp;rd=true" rel="noreferrer">here</a>.</p>