次の関数を呼び出してから 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;
}
}
}
// **********************************************************************************************************************************************************************