How does Garbage Collection in Java work?

后端 未结 9 2162
生来不讨喜
生来不讨喜 2021-02-08 04:36

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

Object A has a reference to Object B and Object B has a reference to Object C. The

相关标签:
9条回答
  • 2021-02-08 05:33

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

    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.

    0 讨论(0)
  • 2021-02-08 05:38

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

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

    0 讨论(0)
  • 2021-02-08 05:40

    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

    0 讨论(0)
提交回复
热议问题