c# - 在C#中监视垃圾收集器

标签 c# .net .net-3.5 garbage-collection

我有一个WPF应用程序,遇到很多性能问题。最糟糕的是,有时应用程序会冻结几秒钟,然后再次运行。

我目前正在调试该应用程序,以查看此冻结可能与之相关,并且我认为可能导致此冻结的原因之一是垃圾收集器。由于我的应用程序在非常有限的环境中运行,因此我相信垃圾收集器在运行时可以使用机器的所有资源,而不会将任何资源留给我们的应用程序。

为了验证这一假设,我找到了以下文章:Garbage Collection NotificationsGarbage Collection Notifications in .NET 4.0,它们解释了如何在垃圾收集器开始运行以及何时完成时通知我的应用程序。

因此,基于这些文章,我创建了以下类以获取通知:

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("===> GC <=== " + Environment.NewLine + "GC is about to begin. Memory before GC: %d", beforeGC);
                    GC.Collect();

                }
                else if (s == GCNotificationStatus.Canceled)
                {
                    LogHelper.Log.Info("===> GC <=== " + Environment.NewLine + "GC about to begin event was cancelled");
                }
                else if (s == GCNotificationStatus.Timeout)
                {
                    LogHelper.Log.Info("===> GC <=== " + Environment.NewLine + "GC about to begin event was timeout");
                }
                else if (s == GCNotificationStatus.NotApplicable)
                {
                    LogHelper.Log.Info("===> GC <=== " + Environment.NewLine + "GC about to begin event was not applicable");
                }
                else if (s == GCNotificationStatus.Failed)
                {
                    LogHelper.Log.Info("===> GC <=== " + 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("===> GC <=== " + Environment.NewLine + "GC has ended. Memory after GC: %d", afterGC);

                    long diff = beforeGC - afterGC;

                    if (diff > 0)
                    {
                        LogHelper.Log.InfoFormat("===> GC <=== " + Environment.NewLine + "Collected memory: %d", diff);
                    }

                }
                else if (s == GCNotificationStatus.Canceled)
                {
                    LogHelper.Log.Info("===> GC <=== " + Environment.NewLine + "GC finished event was cancelled");
                }
                else if (s == GCNotificationStatus.Timeout)
                {
                    LogHelper.Log.Info("===> GC <=== " + Environment.NewLine + "GC finished event was timeout");
                }
                else if (s == GCNotificationStatus.NotApplicable)
                {
                    LogHelper.Log.Info("===> GC <=== " + Environment.NewLine + "GC finished event was not applicable");
                }
                else if (s == GCNotificationStatus.Failed)
                {
                    LogHelper.Log.Info("===> GC <=== " + 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(() =>
        {
            while (true)
            {
                List<char[]> lst = new List<char[]>();

                try
                {
                    for (int i = 0; i <= 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();
    }
}

并且我已将gcConcurrent选项添加到我的app.config文件中(如下所示):
<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net-net-2.0"/>
  </configSections>

  <runtime>
    <gcConcurrent enabled="false" />
  </runtime>

  <log4net>
    <appender name="Root.ALL" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="../Logs/Root.All.log"/>
      <param name="AppendToFile" value="true"/>
      <param name="MaxSizeRollBackups" value="10"/>
      <param name="MaximumFileSize" value="8388608"/>
      <param name="RollingStyle" value="Size"/>
      <param name="StaticLogFileName" value="true"/>
      <layout type="log4net.Layout.PatternLayout">
      <param name="ConversionPattern" value="%date [%thread] %-5level - %message%newline"/>
      </layout>
    </appender>
    <root>
      <level value="ALL"/>
      <appender-ref ref="Root.ALL"/>
    </root>
  </log4net>

  <appSettings>
    <add key="setting1" value="1"/>
    <add key="setting2" value="2"/>
  </appSettings>
  <startup>
    <supportedRuntime version="v2.0.50727"/>
  </startup>

</configuration>

但是,无论何时执行该应用程序,似乎都没有发送任何有关垃圾收集器将运行的通知。我已经在DoGCMonitoring中设置了断点,并且似乎永远不会满足条件(s == GCNotificationStatus.Succeeded)和(s == GCNotificationStatus.Succeeded),因此这些ifs语句的内容永远不会执行。

我究竟做错了什么?

注意:我在WPF和.NET Framework 3.5中使用C#。

更新1

使用AllocationTest方法更新了我的GCMonitor测试。此方法仅用于测试目的。我只是想确保分配了足够的内存来强制垃圾回收器运行。

更新2

更新了DoGCMonitoring方法,并对方法WaitForFullGCApproach和WaitForFullGCComplete的返回进行了新检查。从目前为止我所看到的,我的应用程序将直接进入(s == GCNotificationStatus.NotApplicable)条件。因此,我认为我在某处存在一些配置错误,这使我无法获得所需的结果。

可以在here中找到GCNotificationStatus枚举的文档。

最佳答案

在您的代码中的任何地方都看不到 GC.RegisterForFullGCNotification(int,int) 。看起来您正在使用WaitForFullGC[xxx]方法,但从未注册该通知。这可能就是为什么您获得“不适用”状态的原因。

但是,我怀疑GC是您的问题,尽管可能,但我想最好了解其中存在的所有GC模式以及确定正在发生的最佳方法。 .NET中有两种垃圾收集模式:服务器和工作站。它们都收集相同的未使用内存,但是其完成方式却稍有不同。

  • 服务器版本-此模式告诉GC您正在使用服务器端应用程序,并尝试针对这些方案优化集合。它将堆分成几个部分,每个CPU 1个。启动GC后,它将在每个CPU上并行运行一个线程。您确实想要多个CPU才能正常工作。服务器版本的GC使用多个线程,但与下面列出的并发工作站GC模式不同。每个线程的行为都类似于非并发版本。
  • 工作站版本-此模式告诉GC您正在使用客户端应用程序。它表明您拥有比Server版本更多的有限资源,因此只有一个GC线程。但是,Workstation版本有两种配置:并发和非并发。
  • 并发-这是在使用工作站GC时默认打开的版本(WPF应用程序就是这种情况)。 GC始终在单独的线程上运行,该线程始终在应用程序运行时标记要收集的对象。此外,它选择是否在某些代中压缩内存,然后根据性能进行选择。如果完成压缩,它仍然必须冻结所有线程才能运行集合,但是使用此模式时,您几乎不会看到无响应的应用程序。这为使用创造了更好的交互体验,最适合控制台或GUI应用。
  • 非并行-您可以根据需要配置该版本以使用您的应用程序。在这种模式下,GC线程一直休眠直到启动GC,然后它去标记所有垃圾对象树,释放内存并压缩它,同时所有其他线程都被挂起。这可能导致应用程序有时在短时间内无响应。

  • 您不能在并发收集器上注册通知,因为这是在后台完成的。您的应用程序可能没有使用并发收集器(我注意到您在gcConcurrent中禁用了app.config,但是似乎仅用于测试吗?)。如果是这种情况,那么在有大量收集的情况下,您当然可以看到应用程序冻结。这就是为什么他们创建并发收集器的原因。 GC模式的类型可以在代码中部分设置,而在应用程序配置和机器配置中完全设置。

    我们该怎么做才能确切地了解我们的应用程序正在使用什么?在运行时,您可以查询静态GCSettings类(在System.Runtime中)。 GCSettings.IsServerGC会告诉您您是否在服务器版本上运行工作站,而 GCSettings.LatencyMode 可以告诉您是否正在使用并发,非并发或特殊的代码,您必须在此处设置的代码并不适用。我认为这将是一个不错的起点,并可以解释为什么它在您的机器上运行正常,但在生产环境上却无法正常运行。

    在配置文件中,<gcConcurrent enabled="true|false"/><gcServer enabled="true|false"/>控制垃圾收集器的模式。请记住,这可以在您的app.config文件中(位于执行程序集旁边)或在%windir%\Microsoft.NET\Framework\[version]\CONFIG\中的machine.config文件中

    您还可以远程使用Windows性能监视器来访问生产计算机的.NET垃圾收集性能计数器,并查看这些统计信息。您可以对Windows事件跟踪(ETW)进行全部远程操作。对于性能监视器,您需要.NET CLR Memory对象,然后在实例列表框中选择您的应用程序。

    关于c# - 在C#中监视垃圾收集器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9669963/

    相关文章:

    wpf - 自动完成下拉列表 - 数据太多,超时

    c# - 从数据绑定(bind) ListView 中删除项目 : Possible bug?

    c# - 在没有等待的情况下调用异步方法会发生什么?

    c# - 为什么我在 Active Directory 中创建用户时得到 "Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"

    c# - 句柄不支持与 CreateFile 的同步操作

    c# - 从默认网络代理获取 URI

    c# - 在 Response.Redirect() 之后,代码继续执行

    c# - 动态状态机的更多 .net 方法

    c# - 为什么 Console.WriteLine() 使用 ecx 寄存器,尽管 eax 和 ebx 是免费的?

    asp.net - 使用 Informix 进行 SqlBulkCopy