Will GC collect object a and b if they only have reference to each other?

岁酱吖の 提交于 2019-12-23 09:57:01

问题


Will GC collect object a and b if they only have reference to each other? Can you help explain the reason or give a referece doc to explain that logic. Thanks much


回答1:


Yes they will be candidate to GC if no more strong references to it exist.

It's important to note that not just any strong reference will hold an object in memory. These must be references that chain from a garbage collection root. GC roots are a special class of variable that includes :

  • Temporary variables on the stack (of any thread)
  • Static variables (from any class)
  • Special references from JNI native code

See this documentation (§ A.3.4 Unreachable and §A.4.2 Example GC with WeakReference)




回答2:


If objects a and b referencing each other and are not interfering the other objects, they form an isolated island of objects. This kind of groups are also collected by the garbage collector. Take a look at this thread.



来源:https://stackoverflow.com/questions/10861231/will-gc-collect-object-a-and-b-if-they-only-have-reference-to-each-other

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!