How does a garbage collector avoid an infinite loop here?
c#, garbage-collection
Solution
As per Richter in the second edition of CLR via C# (yes I need to update):
Page 478
For (The CLR is shutting down) each Finalize method is given approximately two seconds to return. If a Finalize method doesn't return within two seconds, the CLR just kills the process - no more Finalize methods are called. Also, if it takes more then 40 seconds to call all objects' Finalize methods, again, the CLR just kills the process.
Also, as Servy mentions, it has its own thread.
Problem
Consider the following C# program, I submitted it on codegolf as an answer to create a loop without looping: ``` class P{ static int x=0; ~P(){ System.Console.WriteLine(++x); new P(); } static void Main(){ new P(); } } ``` This program looks like an infinite loop in my inspection, but it seems to run for several thousand iterations, and then the program terminates successfully without error (No errors are thrown). Is it a spec violation that the finalizer for `P` eventually is not called? Clearly this is stupid code, that should never appear, but I am curious as to how the program could ever complete. Original code golf post:: https://codegolf.stackexchange.com/questions/33196/loop-without-looping/33218#33218