System.Diagnostics.Process causig memory leak

  • Thread starter Sebastian Sosna
  • Start date
S

Sebastian Sosna

Hello NG,

iam having a problem with the code block below. All what i do is to
trace processes that exited. Iam using a 5 sec timer to call the
following Method:
(wich i think is causing the leak)

private void GetRunningProcesses(){

System.Diagnostics.Process[] p =
System.Diagnostics.Process.GetProcesses();


for(int i = 0; i<p.Length ; i++){

p.EnableRaisingEvents = true;
p.Exited += new System.EventHandler(MyProcess_Exited);


}


p = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

}//end

The Method is running in a service. Onstart i call the timer to call
TimerCallbackProc in a 5 sek intervall like this:

OnStart(){
Proctimer = new System.Timers.Timer(5000);
Proctimer.Elapsed+=new
System.Timers.ElapsedEventHandler(TimerCallbackProc);
Proctimer.Enabled=true;
}

The timer callback is triggering GetRunningProcesses()

private void TimerCallbackProc(object source,
System.Timers.ElapsedEventArgs e)
{
GetRunningProcesses();
}

There is no other code wich iam calling in the service only the
methods you see, but still the application is growing and not freeing
any memory!

Can anybody tell me what iam doing wrong? As i said the code is
running in a service and keeps growing in memory usage :(

thanks for advice

regards Sebastian
 
B

Bob

Are you sure you have a leak. .NET does garbage collection
very late. Also, what is the purpose of:


p.EnableRaisingEvents = true;
p.Exited += new System.EventHandler(MyProcess_Exited);


Seems to me you're stacking up handlers here.
 
S

Sebastian Sosna

Hello Bob,

thanks for reply. Ehhm yes your right GC is collecting, but too late!
I ran 2 days the app looking at it right now being stable at 14 mb.
But the highest peeks where about 80 mb!!!! Thats too much.
How can I force the GC to collect not referenced events lets say every
hour? Is that possible?

Thanks!

Regards
Sebastian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top