Do Timer object get GC-ed when no other object ref

2019-01-26 11:41发布

Can an object containing an active Timer (System.Timers.Timer) be garbage collected, if no other object is referencing it?

2条回答
你好瞎i
2楼-- · 2019-01-26 12:12

Yes. Timer will be GC'd if left unreferenced, even while running.

The documentation says:

The code contains declarations of the timer variable at the class level and inside Main. To see how aggressive garbage collection can affect a timer that is declared inside a long-running method, you can comment out the class-level declaration and uncomment the local variable. To keep the timer from being collected, uncomment the GC.KeepAlive method at the end of Main.

查看更多
霸刀☆藐视天下
3楼-- · 2019-01-26 12:13

There are two basic ways that a Timer stays referenced even if you don't store a reference somewhere:

  • As long as the timer is enabled. There's an underlying System.Threading.Timer that actually generates the event. The CLR keeps a list of active timers, the callback delegate object of them keeps the Timer referenced.

  • By the Elapsed event handler if it is not static. That extends the lifetime of the Timer instance to at least the lifetime of the class object that contains the Elapsed event handler.

There is no failure mode here. The only possible way to get a reference back to the timer is through the Elapsed event handler's sender argument. If you disable the timer then there is no longer a way to get a reference so it is appropriate for the object to be collected.

A fairly classic way to get in trouble with this Timer and lose Elapsed events is when you have the AutoReset property set to false. A very nasty issue is that any exceptions raised in the Elapsed event handler are swallowed without any diagnostic. Which will bypass the call you'd have to restart the timer at the end. Do favor System.Threading.Timer, it doesn't have this problem.

查看更多
登录 后发表回答