1
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...

Ry-
  • 218,210
  • 55
  • 464
  • 476
Machinarius
  • 3,637
  • 3
  • 30
  • 53
  • 2
    The task manager is not the correct tool to determine memory leaks. A profiler would be a better tool. [Check here for more details](http://stackoverflow.com/questions/104/anatomy-of-a-memory-leak). – Fredrik Mörk Jun 25 '11 at 21:03
  • first of all, you should use a temporary variable like var handler = WorkerEvent and then check the handler is null. – Shuhel Ahmed Jun 25 '11 at 21:05
  • As a side note, don't you have to mark the fields `Work` and `Running` as `volatile`, since apparently they will be modified by a thread outside the `while(Work)` loop? – Bruno Reis Jun 25 '11 at 21:07

4 Answers4

2

It could be because you're keeping the entire array. You dispose each Process object 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:

Processes = null;
GC.Collect();
Ry-
  • 218,210
  • 55
  • 464
  • 476
0

In C# memory isn't deallocated immediately, even when Dispose is called. It is freed up at a later point in time by the garbage collector. The garbage collector is regularly run automatically in the background. If you can see that it continues to use more and more memory over a long time (10th of minutes) you can start worrying about a resource leak.

Anders Abel
  • 67,989
  • 17
  • 150
  • 217
0

.Net applications are 'free' to claim as much memory as needed and release it only when needed. 'Leaking' is commonly referred to as allocated memory that can't be freed. The disposed objects could be freed later when needed.

Emond
  • 50,210
  • 11
  • 84
  • 115
0

new WorkerEventArgs(_Event): where do you dispose it?

public virtual void OnWorkerEvent(String _Event)
{
    if (WorkerEvent != null) WorkerEvent(this, new WorkerEventArgs(_Event) );
}
Ry-
  • 218,210
  • 55
  • 464
  • 476
William R
  • 185
  • 1
  • 10