Memory Leak in System.Timers.Timer?

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
 
G

Guest

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!
***************************
 
G

Guest

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!
 
S

Sean Hederman

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.
 

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