是否收集了 Java 线程垃圾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2423284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Thread Garbage collected or not
提问by Ashish
This question was posted on some site. I didnt find right answers there, so I am posting it here again.
这个问题发布在某个网站上。我没有在那里找到正确的答案,所以我再次在这里发布。
public class TestThread {
public static void main(String[] s) {
// anonymous class extends Thread
Thread t = new Thread() {
public void run() {
// infinite loop
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
// as long as this line printed out, you know it is alive.
System.out.println("thread is running...");
}
}
};
t.start(); // Line A
t = null; // Line B
// no more references for Thread t
// another infinite loop
while (true) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
System.gc();
System.out.println("Executed System.gc()");
} // The program will run forever until you use ^C to stop it
}
}
My query is not about stopping a Thread. Let me rephrase my question. Line A(see code above) starts a new Thread; and Line B make the thread reference null. So, the JVM now has a Thread Object(which is in running state) to which no reference exists (as t=null in Line B). So my question is, why does this thread(that has no reference anymore in the main thread) keeps on running until the main thread is running. Per my understanding, the thread object should have been garbage collected post Line B. I tried to run this code for 5 minutes and more, requesting Java Runtime to run GC, but the thread just does not stop.
我的查询不是关于停止线程。让我重新表述我的问题。A行(见上面的代码)开始一个新的线程;和 B 行使线程引用为空。因此,JVM 现在有一个不存在引用的线程对象(处于运行状态)(如 B 行中的 t=null)。所以我的问题是,为什么这个线程(在主线程中不再有引用)一直运行直到主线程运行。根据我的理解,线程对象应该在 B 行后被垃圾收集。我尝试运行这段代码 5 分钟或更长时间,请求 Java Runtime 运行 GC,但线程并没有停止。
Hope both the code and question is clear this time.
希望这次代码和问题都很清楚。
采纳答案by falstro
A running thread is considered a so called garbage collection root and is one of those things keeping stuff from being garbage collected. When the garbage collector determines whether your object is 'reachable' or not, it is always doing so using the set of garbage collector roots as reference points.
正在运行的线程被认为是所谓的垃圾收集根,并且是防止垃圾收集的东西之一。当垃圾收集器确定您的对象是否“可达”时,它总是使用垃圾收集器根集作为参考点。
Consider this, why is your main thread not being garbage collected, no one is referencing that one either.
考虑一下,为什么您的主线程没有被垃圾收集,也没有人引用该线程。
回答by Thilo
The JVM has a reference to all running threads.
JVM 引用了所有正在运行的线程。
No thread (or the things it refers to) will be garbage-collected while it is still running.
任何线程(或它所指的东西)在它仍在运行时都不会被垃圾收集。
回答by vy32
The Thread is not garbage collected because there are references to the threads that you cannot see. For example, there are references in the runtime system.
线程不会被垃圾回收,因为存在对您看不到的线程的引用。例如,运行时系统中有引用。
When the Thread is created it is added to the current thread group. You can get a list of Threads in the current thread group, so that is another way to get a reference to it.
创建线程后,它会被添加到当前线程组中。您可以获取当前线程组中的线程列表,因此这是获取对它的引用的另一种方法。
回答by Thomas Pornin
As was explained, running threads are, by definition, immune to GC. The GC begins its work by scanning "roots", which are deemed always reachable; roots include global variables ("static fields" in Java-talk) and the stacks of all running threads (it can be imagined that the stack of a running thread references the corresponding Thread
instance).
如前所述,根据定义,正在运行的线程不受 GC 影响。GC 通过扫描“根”开始其工作,根被认为总是可达的;根包括全局变量(Java-talk中的“静态字段”)和所有正在运行的线程的堆栈(可以想象一个正在运行的线程的堆栈引用了相应的Thread
实例)。
However, you can make a thread a "daemon" thread (see Thread.setDaemon(boolean)
). A daemon thread is no more garbage-collected than a non-daemon thread, but the JVM exits when all running threads are daemon. One way to imagine it is that every thread, when it terminates, checks whether there remain some non-daemon running threads; if not, the terminating thread forces a System.exit()
call, which exits the JVM (killing off the running daemon threads). This is not a GC-related issue; in a way, threads are allocated manually. However, this is how the JVM can tolerate semi-rogue threads. This is typically used for Timer
instances.
但是,您可以使线程成为“守护程序”线程(请参阅 参考资料Thread.setDaemon(boolean)
)。守护线程不会比非守护线程更多地进行垃圾收集,但是当所有正在运行的线程都是守护线程时,JVM 就会退出。一种想象方式是,每个线程在终止时,会检查是否还有一些非守护进程正在运行的线程;如果没有,终止线程会强制System.exit()
调用,从而退出 JVM(终止正在运行的守护线程)。这不是 GC 相关的问题;在某种程度上,线程是手动分配的。但是,这就是 JVM 可以容忍半流氓线程的方式。这通常用于Timer
实例。