轮询线程消耗大量内存

时间:2013-08-13 19:52:27

标签: c# multithreading memory-management visual-studio-2012

我有一个轮询线程,它调用以下函数,然后休眠20秒左右。我一直试图确定为什么我的应用程序(C#表单)在应用程序持续时间内累积了高达500Kbs的内存要求。当我删除轮询线程时,它似乎在应用程序的持续时间内有一个小的常量内存使用量。我得出结论,线程中的accessControl方法在离开作用域时没有适当地释放资源。有谁熟悉这个?

    // Method that accesses Form objects that must be accessed by original thread
    private void accessControl()
    {
        int secCount = 600;
        bool[] newViolation;
        bool tempBool = false;
        tempBool = label3.InvokeRequired || labelSecondLargest.InvokeRequired || labelThirdLargest.InvokeRequired;
        if (tempBool)
        {
            System.Console.WriteLine("CHANGING LABELS");
            labelLargest.Invoke(new MethodInvoker(delegate
            {
                newViolation = checkViolations();
                Draw(Assets);
                Thread.Sleep(secCount);
                int counter = 0;
                int duration = 3;
                while(counter < duration)
                {
                    if(newViolation[0])
                        labelLargest.ForeColor = System.Drawing.Color.Red;
                    if (newViolation[1])
                        labelSecondLargest.ForeColor = System.Drawing.Color.Red;
                    if (newViolation[2])
                        labelThirdLargest.ForeColor = System.Drawing.Color.Red;

                    System.Windows.Forms.Application.DoEvents();
                    Thread.Sleep(secCount);                         // Wait secCount/1000 seconds before moving on...
                    if (newViolation[0])
                        labelLargest.ForeColor = System.Drawing.Color.Blue;
                    if (newViolation[1])
                        labelSecondLargest.ForeColor = System.Drawing.Color.Green;
                    if (newViolation[2])
                        labelThirdLargest.ForeColor = System.Drawing.Color.Purple;
                    counter++;                                      // Do this 'counter' many times
                }
            }));
        }
        else
        {
           System.Console.WriteLine("CHANGING LABELS 2");
           newViolation = checkViolations();
           Draw(Assets);
           Thread.Sleep(secCount);
           int counter = 0;
           int duration = 3;
           while(counter < duration)
           {
                if(newViolation[0])
                    labelLargest.ForeColor = System.Drawing.Color.Red;
                if (newViolation[1])
                    labelSecondLargest.ForeColor = System.Drawing.Color.Red;
                if (newViolation[2])
                    labelThirdLargest.ForeColor = System.Drawing.Color.Red;

                System.Windows.Forms.Application.DoEvents();
                Thread.Sleep(secCount);                         // Wait secCount/1000 seconds before moving on...
                 if (newViolation[0])
                    labelLargest.ForeColor = System.Drawing.Color.Blue;
                 if (newViolation[1])
                    labelSecondLargest.ForeColor = System.Drawing.Color.Green;
                 if (newViolation[2])
                     labelThirdLargest.ForeColor = System.Drawing.Color.Purple;
                }
            }
        }
    // **********************************************************************************************************************************************************************

1 个答案:

答案 0 :(得分:0)

正如有人提到的,Draw(Assets)方法是问题的根源。该方法负责声明和实例化表示winform中不同选项卡的新DataGridView对象。

然而,这总是创建新的DataGridView对象而没有正确删除旧的(我认为,因为这是一个表单属性,正常的内存管理不会在Draw()的范围之外应用一次)。

为了解决这个问题,我将这些变量设为全局变量,并仅在不存在(null)时对它们进行实例化。