1

Hey I have this console application, which needs to run over longer periods of time, one of it functions is to detect a certain process, but it's leaking on that part.

I'm using this in a timer for the detection:

Private Sub OnTimedEvent() Handles mytimer.Elapsed
        If Process.GetProcessesByName(Gname).Length = 1 Then
            misRunning = True
        Else
            misRunning = False
        End If
    End Sub

GC.Collect would help, but I'm not certain that calling GC.Collect is the right approach.

sloth
  • 99,095
  • 21
  • 171
  • 219
user3264418
  • 11
  • 1
  • 4
  • There doesn't look like there is anything to leak here - why do you suspect this is the code that is causing the leak? – Matt Wilko Feb 05 '14 at 12:51
  • have you read this article - http://stackoverflow.com/questions/13084585/process-getprocessesbynamestring-string-memory-leak? – Konrad Kokosa Feb 05 '14 at 12:54
  • Beacause it says in ANTS Memory Profiler it's there – user3264418 Feb 05 '14 at 13:02
  • Info: MS Connect entry for this: http://connect.microsoft.com/VisualStudio/feedback/details/768814/memory-leak-in-process-getprocessesbyname-string-string states that calling `PerformanceCounter.CloseSharedResources` is a workaround – Matt Wilko Feb 05 '14 at 13:03
  • Yea but using it in a module doesn't work..... You can't use Shared in modules. I tryed making a Public class to handle it, getting called from the module, but didn't work either. – user3264418 Feb 05 '14 at 13:04
  • Just because the profiler says you have a leak doesn't necessarily mean you have a 'leak' you probably just have objects that have not been garbage collected yet. What happens if you pause execution and wait five minutes? – Matt Wilko Feb 05 '14 at 13:08
  • Hmm what's the difference? and what do you mean by pause? pause the debugger and then watch if it leaks? If I run it for 3 hours it gets to 1GB, with GC.Collect only 10mb all time. – user3264418 Feb 05 '14 at 16:41

1 Answers1

-1

Possible re-entrancy issue in your code OnTimedEvent fires before the previous call has finished

Try disabling the timer when you enter the method and re-enabling it afterwards:

Private Sub OnTimedEvent() Handles mytimer.Elapsed
    myTimer.Enabled = False
    If Process.GetProcessesByName(Gname).Length = 1 Then
        misRunning = True
    Else
        misRunning = False
    End If
    myTimer.Enabled = True
End Sub
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143