Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Timers and Garbage Collection

According to MSDN, a reference to System.Threading.Timer should be kept otherwise it will get garbage-collected. So if I run this code, it doesn't write any message (which is the expected behavior):

static void Main(string[] args)
{
    RunTimer();
    GC.Collect();
    Console.ReadKey();
}

public static void RunTimer()
{
    new Timer(s => Console.WriteLine("Hello"), null, TimeSpan.FromSeconds(1), TimeSpan.Zero);
}

However, if I modify the code slightly by storing the timer in a temporary local variable, it survives and writes the message:

public static void RunTimer()
{
    var timer = new Timer(s => Console.WriteLine("Hello"));
    timer.Change(TimeSpan.FromSeconds(1), TimeSpan.Zero);
}

During garbage collection, there is apparently no way how to access the timer from root or static objects. So can you please explain why the timer survives? Where is the reference preserved?

like image 243
Peter Smolinsky Avatar asked Jun 07 '16 14:06

Peter Smolinsky


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C language?

C is a high-level and general-purpose programming language that is ideal for developing firmware or portable applications. Originally intended for writing system software, C was developed at Bell Labs by Dennis Ritchie for the Unix Operating System in the early 1970s.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

Each Timer references a TimerHolder, which references a TimerQueueTimer. The implementation keeps an internal reference to the TimerQueueTimer via the call to UpdateTimer().

Under normal circumstances your timer is free to be collected, finalizing the TimerHolder and removing the TimerQueueTimer from the internal queue. But the simple constructor Timer(TimerCallback) calls TimerSetup() with the Timer itself as the state. So in this particular case, the TimerQueueTimer's state references back to the Timer, preventing it from being collected.

The effect is not related to keeping a temporary local variable. It just so happens to work due to the internals of the Timer mechanism. It is much clearer and safer to keep a reference to your timer as recommended by MSDN.

like image 183
piedar Avatar answered Oct 01 '22 18:10

piedar