<p>In .NET, after this code, what mechanism stops the <code>Thread</code> object from being garbage collected?</p>

<pre><code>new Thread(Foo).Start();
GC.Collect();
</code></pre>

<p>Yes, it's safe to assume <strong>something</strong> has a reference to the thread, I was just wandering what exactly. For some reason Reflector doesn't show me <code>System.Threading</code>, so I can't dig it myself (I know MS released the source code for the .NET framework, I just don't have it handy).</p>

[{'answer_id': 81739, 'author': 'Jon', 'author_id': 12261, 'author_profile': 'https://Stackoverflow.com/users/12261', 'pm_score': 0, 'selected': False, 'text': "<p>Well, it's safe to assume that if a thread is running somewhere that something has a reference to it so wouldn't that be enough to stop the garbage collection?</p>\n"}, {'answer_id': 81755, 'author': 'Grad van Horck', 'author_id': 12569, 'author_profile': 'https://Stackoverflow.com/users/12569', 'pm_score': -1, 'selected': False, 'text': '<p>Assign the new Thread to a local field?</p>\n\n<pre><code>class YourClass\n{\n  Thread thread;\n\n  void Start()\n  {\n    thread = new Thread(Foo);\n    thread.Start();\n    GC.Collect();\n  }\n}\n</code></pre>\n\n<p>Garbage Collection collects everyting that is not references, so in your code there is no field/variable referencing to the thread, so it will be collected.</p>\n'}, {'answer_id': 81763, 'author': 'EricSchaefer', 'author_id': 8976, 'author_profile': 'https://Stackoverflow.com/users/8976', 'pm_score': 5, 'selected': True, 'text': '<p>The runtime keeps a reference to the thread as long as it is running. The GC wont collect it as long as anyone still keeps that reference.</p>\n'}, {'answer_id': 81805, 'author': 'ima', 'author_id': 5733, 'author_profile': 'https://Stackoverflow.com/users/5733', 'pm_score': 2, 'selected': False, 'text': "<p>It's a hard-wired feature of garbage collector. Running threads are not collected.</p>\n"}, {'answer_id': 100763, 'author': 'Damien_The_Unbeliever', 'author_id': 15498, 'author_profile': 'https://Stackoverflow.com/users/15498', 'pm_score': 0, 'selected': False, 'text': "<p>Important point to note though - if your thread is marked with IsBackground=True, it won't prevent the whole process from exiting</p>\n"}, {'answer_id': 100850, 'author': 'Ilya Ryzhenkov', 'author_id': 18575, 'author_profile': 'https://Stackoverflow.com/users/18575', 'pm_score': 3, 'selected': False, 'text': '<p>It depends on whether the thread is running or not. If you just created Thread object and didn\'t start it, it is an ordinary managed object, i.e. eligible for GC. As soon as you start thread, or when you obtain Thread object for already running thread (GetCurrentThread) it is a bit different. The "exposed object", managed Thread, is now hold on strong reference within CLR, so you always get the same instance. When thread terminates, this strong reference is released, and the managed object will be collected as soon as you don\'t have any other references to (now dead) Thread.</p>\n'}]
['https://Stackoverflow.com/questions/81730', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/11236/']