How does Garbage Collection in Java work?

后端 未结 9 2139
生来不讨喜
生来不讨喜 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.

提交回复
热议问题