v3ks

I am running an application, and after a long period of time, it slows down significantly and I am not sure what is causing it.
After reading several discussions, I have come to the realization that I might be leaking memory without knowing it.

I am running a main application, and there is a thread that is running at an average of 10 times/second performing some task.

Do I leak memory every time I call the "new thread" or the garbage collector will clean the thread every time it is dies

Thanks!

// my thread definition
public void ComputeAverage()
{
// do averaging on an array and store avg in a variable
Thread.Sleep(100); //This is done so I can obtain around 10 averages/second
}



public void timer1_tick(object sender, System.EventArgs e)
{
timer1.Enabled = false;

if(avgThread == null || avgThread.IsAlive == false)
{
// make sure the array list doesn't grow infinitally
if(myArrayList.Count => 100)
myArrayList.Clear();

// add a new value to the array
myArrayList.Add( /*I will add a random number between 1 and 100 */ );

avgThread = new Thread (new ThreadStart(ComputeAverage));
avgThread.Start();
}

// do more work.

// enable timer
timer1.Enabled = true;
}


Re: Visual C# General is my code leaking memory?

H. Tony


you can easily allocate too much new memory through all those operations without 'leaking' any memory, try a forced GC.Collect() with GC.WaitForPendingFinalizers() from time to time so that GC.Collect() can catch up with your memory allocation.





Re: Visual C# General is my code leaking memory?

fubak

I've just started reading about threading myself, but one rule that I read was to try avoiding creating threads manually and just 'lease' threads from the thread pool instead.




Re: Visual C# General is my code leaking memory?

nobugz

The Thread class should have a Dispose() method but doesn't have one. It will leak 4 handles that do not get released until the garbage collector runs. If it never runs, or doesn't run frequently enough, you'll leak unmanaged resources badly. Use ThreadPool.QueueUserWorkItem() to avoid this. In your specific example, there isn't a good reason to create a new thread over and over again. Just use one that runs a loop on a ManualResetEvent.





Re: Visual C# General is my code leaking memory?

H. Tony

nobugz wrote:
The Thread class should have a Dispose() method but doesn't have one. It will leak 4 handles that do not get released until the garbage collector runs. If it never runs, or doesn't run frequently enough, you'll leak unmanaged resources badly. Use ThreadPool.QueueUserWorkItem() to avoid this. In your specific example, there isn't a good reason to create a new thread over and over again. Just use one that runs a loop on a ManualResetEvent.


I am just curious. what are the states for those 4 handles since they can be collected when garbage colletor runs, why is it considered as 'leak'

many thanks.





Re: Visual C# General is my code leaking memory?

nobugz

They will be released by a finalizer after the garbage collector runs. You just can't Dispose() them.





Re: Visual C# General is my code leaking memory?

OmegaMan

To add to the other posters; See this article Identify And Prevent Memory Leaks In Managed Code to begin understand what to look for and how to use the resources available when identifying memory leaks. Good Luck HTH.





Re: Visual C# General is my code leaking memory?

v3ks

Thank you so much.