public class Worker
{
private Boolean Running = false;
public Boolean Work = true;
private Process[] Processes;
public event EventHandler<WorkerEventArgs> WorkerEvent;
public virtual void OnWorkerEvent(String _Event)
{
if (WorkerEvent != null) WorkerEvent(this, new WorkerEventArgs(_Event));
}
public void Start()
{
while (Work)
{
Processes = Process.GetProcessesByName("iw4mp.dat");
if (Processes.Count() >= 1)
{
if (!Running)
{
OnWorkerEvent("Run");
}
Running = true;
Thread.Sleep(2500);
}
else
{
if (Running)
{
OnWorkerEvent("Exit");
}
Running = false;
Thread.Sleep(2500);
}
foreach (var A in Processes)
{
A.Dispose();
}
}
}
}
This class is leaking memory each 2.5 seconds (Yes, i monitored the memory usage with the Task Manager) when i call a ThreadStart with the Start() function. Any ideas on why is this happening…?
Basically, the Start() method should just poll if iw4mp.dat is running, even though it works… i have no idea why it keeps allocating memory each loop…
It could be because you’re keeping the entire array. You dispose each
Processobject in the array at the end of the loop, but the array itself and all of its disposed (but not garbage collected) elements remain in memory. The garbage collector can come into action at arbitrary times, so if your system is not out of memory then it could be simply because the GC has not yet decided to collect.If you really want to force the collection, put this at the end of your loop: