Java中的垃圾收集是如何工作的?

时间:2021-05-07 03:49:36

I was wondering how the garbage collector in Java deals with the following situation.

我想知道Java中的垃圾收集器如何处理以下情况。

Object A has a reference to Object B and Object B has a reference to Object C. The main program has a reference to Object A. So you can use Object B trough Object A, and Object C trough Object B trough Object A.

对象A对对象B有引用,对象B对对象C有引用。主程序对对象A有引用,所以可以用对象B对对象A,用对象C对对象B对对象A有引用。

What happens to Object B and Object C, if the link between Object A and Object B is set to null?

如果对象A和对象B之间的链接被设置为null,那么对象B和对象C会发生什么?

Should Object B and Object C now been collected by the Garbage Collector? I mean there is still a connection between Object B and Object C.

对象B和对象C是否应该由垃圾收集器收集?我的意思是物体B和物体C之间仍然有联系。

9 个解决方案

#1


9  

Should Object B and Object C now been collected by the Garbage Collector?

对象B和对象C现在应该由垃圾收集器收集吗?

Yes. Well, they are candidates for collection because there's no way to reach Object B and C through the root that is A.

是的。它们是集合的候选对象因为无法通过根A到达B和C。

#2


6  

Yes, B and C are eligible for garbage collection, if they can't be reached from any GC root (GC roots are usually all Threads and all references on the stack).

是的,如果不能从任何GC根(GC根通常是堆栈上的所有线程和所有引用)访问垃圾收集,那么B和C是合格的。

#3


4  

You can't count on the garbage collector to work at a specific time,since its behavior is unpredictable,all you can say is that objects B and C are only eligible for garbage collection

您不能指望垃圾收集器在特定的时间工作,因为它的行为是不可预测的,您只能说对象B和C只能用于垃圾收集

#4


4  

As usual, this article is a must-read for whoever wants to understand what garbage collection does. It is well-written and has explanatory drawings.

像往常一样,这篇文章对于任何想了解垃圾收集的人来说都是必读的。它写得很好,有解释性的图画。

#5


2  

In fact, garbage collection in java is a very sophisticated thing, far more than in Ruby interpreter, as an example.

事实上,java中的垃圾收集是非常复杂的事情,远比Ruby解释器中的垃圾收集复杂得多。

Anyway, the theoretical basis is the same.

无论如何,理论基础是一样的。

The GC identifies objects graphs that are no more reachable by program code (that's to say they have no more reference in active code). When talking about object graph, I precisely talk about B->C object graph. once it is unreachable, it can be GC'ed, but you can't tell when it will be, due to the GC trying to optimize as much as possible its work to avoid slowing the application down.

GC标识程序代码不再可访问的对象图(也就是说,它们在活动代码中不再具有引用)。当谈到对象图时,我确切地说的是B- >c对象图。一旦它不可访问,就可以对它进行GC处理,但是您不能确定它将在什么时候到达,因为GC试图尽可能地优化它的工作,以避免使应用程序变慢。

#6


2  

B and C are eligable for garbage collection because you can't access them any more. With the unpredicatbility of the garbage collector all we know is they are quite likely to get collected at some point in the future.

B和C可用于垃圾收集,因为您再也不能访问它们了。由于垃圾收集器的不可预测性,我们所知道的是它们很可能在将来的某个时候被收集。

#7


1  

I think the logic is different. If the object is not accessible from a thread then it can be collected.

我认为逻辑是不同的。如果对象不能从线程访问,则可以收集它。

#8


1  

If there is no reference to object, then it will be suitable for GC to proceed

如果没有对对象的引用,则适合GC继续进行

#9


0  

B has no reference to it so it will be garbage collected first, then it will understand that C has no reference to it, so C will be garbage collected. It is for illustration, Jvm is smart enough to scoop them in one sweep.

B没有对它的引用,所以它将首先被垃圾收集,然后它会理解C没有对它的引用,所以C将被垃圾收集。这只是为了说明,Jvm足够聪明,可以一次将它们一网打尽。

#1


9  

Should Object B and Object C now been collected by the Garbage Collector?

对象B和对象C现在应该由垃圾收集器收集吗?

Yes. Well, they are candidates for collection because there's no way to reach Object B and C through the root that is A.

是的。它们是集合的候选对象因为无法通过根A到达B和C。

#2


6  

Yes, B and C are eligible for garbage collection, if they can't be reached from any GC root (GC roots are usually all Threads and all references on the stack).

是的,如果不能从任何GC根(GC根通常是堆栈上的所有线程和所有引用)访问垃圾收集,那么B和C是合格的。

#3


4  

You can't count on the garbage collector to work at a specific time,since its behavior is unpredictable,all you can say is that objects B and C are only eligible for garbage collection

您不能指望垃圾收集器在特定的时间工作,因为它的行为是不可预测的,您只能说对象B和C只能用于垃圾收集

#4


4  

As usual, this article is a must-read for whoever wants to understand what garbage collection does. It is well-written and has explanatory drawings.

像往常一样,这篇文章对于任何想了解垃圾收集的人来说都是必读的。它写得很好,有解释性的图画。

#5


2  

In fact, garbage collection in java is a very sophisticated thing, far more than in Ruby interpreter, as an example.

事实上,java中的垃圾收集是非常复杂的事情,远比Ruby解释器中的垃圾收集复杂得多。

Anyway, the theoretical basis is the same.

无论如何,理论基础是一样的。

The GC identifies objects graphs that are no more reachable by program code (that's to say they have no more reference in active code). When talking about object graph, I precisely talk about B->C object graph. once it is unreachable, it can be GC'ed, but you can't tell when it will be, due to the GC trying to optimize as much as possible its work to avoid slowing the application down.

GC标识程序代码不再可访问的对象图(也就是说,它们在活动代码中不再具有引用)。当谈到对象图时,我确切地说的是B- >c对象图。一旦它不可访问,就可以对它进行GC处理,但是您不能确定它将在什么时候到达,因为GC试图尽可能地优化它的工作,以避免使应用程序变慢。

#6


2  

B and C are eligable for garbage collection because you can't access them any more. With the unpredicatbility of the garbage collector all we know is they are quite likely to get collected at some point in the future.

B和C可用于垃圾收集,因为您再也不能访问它们了。由于垃圾收集器的不可预测性,我们所知道的是它们很可能在将来的某个时候被收集。

#7


1  

I think the logic is different. If the object is not accessible from a thread then it can be collected.

我认为逻辑是不同的。如果对象不能从线程访问,则可以收集它。

#8


1  

If there is no reference to object, then it will be suitable for GC to proceed

如果没有对对象的引用,则适合GC继续进行

#9


0  

B has no reference to it so it will be garbage collected first, then it will understand that C has no reference to it, so C will be garbage collected. It is for illustration, Jvm is smart enough to scoop them in one sweep.

B没有对它的引用,所以它将首先被垃圾收集,然后它会理解C没有对它的引用,所以C将被垃圾收集。这只是为了说明,Jvm足够聪明,可以一次将它们一网打尽。