How does Java Garbage Collection work with Circular References?

后端 未结 8 1839
旧时难觅i
旧时难觅i 2020-11-22 04:53

From my understanding, garbage collection in Java cleans up some objects if nothing else is \'pointing\' to that object.

My question is, what happens if we have some

8条回答
  •  迷失自我
    2020-11-22 05:41

    Garbage collection doesn't usually mean "clean some object iff nothing else is 'pointing' to that object" (that's reference counting). Garbage collection roughly means finding objects that can't be reached from the program.

    So in your example, after a,b, and c go out of scope, they can be collected by the GC, since you can't access these objects anymore.

提交回复
热议问题