Memory Leak in System.Timers.Timer?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I made a Windows Service in VS.Net 2003 with VB. I've included the main
code, there's some other code, but it's rem'ed out and essentially just a
timer running here. I'm trying to run something at 5:20 every day. I caught
a comment in a different thread about system.threads.timer perhaps being a
better way of handling the situation, but anyway...

When I start the service, I see that the memory usage in Task Manager is
7,124. In 30 minutes it's 7,232. 15 hours later it's 7,640 K. It's
leveling off, but I guess I am wondering why it's getting larger at all.

Perhaps a second question (related), is if this is the best way to run
something at a specific time.

Dim timerClean As New System.Timers.Timer

Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
boolAlreadyRan = True
dateRunTime = TimeValue("5:20:00 pm")
' Set the timer to check every 5 minutes.
StartTimer(timerClean, 300000)
EventLog1.WriteEntry("In OnStart")
End Sub 'OnStart

Private Sub StartTimer(ByRef aTimer As System.Timers.Timer, ByVal
Interval As Integer)
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
aTimer.Interval = Interval
aTimer.Enabled = True
End Sub

Private Sub StopTimer(ByRef aTimer As System.Timers.Timer)
aTimer.Enabled = False
End Sub

Private Sub OnTimedEvent(ByVal source As Object, ByVal e As
ElapsedEventArgs)
If (boolAlreadyRan = False) And (TimeValue(Now) > dateRunTime) Then
'Timer Event Occurred.
boolAlreadyRan = True
End If
If (boolAlreadyRan = True) And (TimeValue(Now) > dateRunTime) And
(TimeValue(Now) < dateRunTime.AddMinutes(6)) Then
boolAlreadyRan = False
End If

End Sub 'OnTimedEvent
 
I would venture that it would be more efficient to use the Windows scheduler
command (at.exe), even if you ended up having to put a COM wrapper around it.


---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
Yes, yes it would, especially in what I would like this particular program to
do.

Hope I'm not going to sound rude, but I'm not sure if it answered the
question if there was a problem with a system.timers.timer running in a
service, as I assume you would almost always have in a service. If so why
does memory keep getting used up or, if let go long enough would eventually
stop growing.

Thanks!
 
System.Timers.Timer is pretty much just a wrapper over
System.Threading.Timer which does some synchronisation and and presents the
timer events as an event rather than a callback. If you're calling StopTimer
elsewhere, please note that it's not calling RemoveHandler on the Elapsed
event handler set up in StartTimer.

Please keep in mind that you shouldn't rely on Task Manager to get an
accurate picture of what .NET apps are doing to memory. Rather use the
Performance Monitor Memory counters.
 
Back
Top