In .NET, after this code, what mechanism stops the Thread
object from being garbage collected?
在.NET中,在此代码之后,什么机制阻止Thread对象被垃圾回收?
new Thread(Foo).Start();
GC.Collect();
Yes, it's safe to assume something has a reference to the thread, I was just wandering what exactly. For some reason Reflector doesn't show me System.Threading
, 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).
是的,假设某些东西有对线程的引用是安全的,我只是在徘徊究竟是什么。出于某种原因,Reflector没有向我展示System.Threading,所以我不能自己挖掘它(我知道MS发布了.NET框架的源代码,我只是没有它的方便)。
6 个解决方案
#1
14
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.
只要运行时,运行时就会保留对线程的引用。只要有人仍然保留该参考,GC就不会收集它。
#2
7
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.
这取决于线程是否正在运行。如果您刚刚创建了Thread对象但没有启动它,则它是一个普通的托管对象,即符合GC条件。一旦启动线程,或者为已经运行的线程(GetCurrentThread)获取Thread对象,它就会有所不同。管理Thread的“公开对象”现在在CLR中保持强引用,因此您始终获得相同的实例。当线程终止时,将释放此强引用,并且只要您没有对(现在已死)Thread的任何其他引用,就会收集托管对象。
#3
2
It's a hard-wired feature of garbage collector. Running threads are not collected.
这是垃圾收集器的硬连线功能。不收集运行线程。
#4
0
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?
好吧,可以安全地假设,如果一个线程在某个地方运行某个东西有引用它,那么这不足以阻止垃圾收集吗?
#5
0
Important point to note though - if your thread is marked with IsBackground=True, it won't prevent the whole process from exiting
需要注意的重要一点 - 如果您的线程标记为IsBackground = True,则不会阻止整个进程退出
#6
-3
Assign the new Thread to a local field?
将新线程分配给本地字段?
class YourClass
{
Thread thread;
void Start()
{
thread = new Thread(Foo);
thread.Start();
GC.Collect();
}
}
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.
垃圾收集收集不是引用的每个,因此在您的代码中没有引用该线程的字段/变量,因此将收集它。
#1
14
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.
只要运行时,运行时就会保留对线程的引用。只要有人仍然保留该参考,GC就不会收集它。
#2
7
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.
这取决于线程是否正在运行。如果您刚刚创建了Thread对象但没有启动它,则它是一个普通的托管对象,即符合GC条件。一旦启动线程,或者为已经运行的线程(GetCurrentThread)获取Thread对象,它就会有所不同。管理Thread的“公开对象”现在在CLR中保持强引用,因此您始终获得相同的实例。当线程终止时,将释放此强引用,并且只要您没有对(现在已死)Thread的任何其他引用,就会收集托管对象。
#3
2
It's a hard-wired feature of garbage collector. Running threads are not collected.
这是垃圾收集器的硬连线功能。不收集运行线程。
#4
0
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?
好吧,可以安全地假设,如果一个线程在某个地方运行某个东西有引用它,那么这不足以阻止垃圾收集吗?
#5
0
Important point to note though - if your thread is marked with IsBackground=True, it won't prevent the whole process from exiting
需要注意的重要一点 - 如果您的线程标记为IsBackground = True,则不会阻止整个进程退出
#6
-3
Assign the new Thread to a local field?
将新线程分配给本地字段?
class YourClass
{
Thread thread;
void Start()
{
thread = new Thread(Foo);
thread.Start();
GC.Collect();
}
}
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.
垃圾收集收集不是引用的每个,因此在您的代码中没有引用该线程的字段/变量,因此将收集它。