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

Will the GC collect objects a and b if they only have a reference to each other? Can you explain the reason or give a reference doctor to explain this logic. Many thanks

+5
source share
2 answers

Yes, they will be a candidate for the GC if there are no stronger references to it.

It is important to note that not only a strong link will contain an object in memory. These should be links to the chain from the root of the garbage collection . GC Roots is a special class of variable that includes:

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

. (§ A.3.4 §A.4.2 GC WeakReference)

+6

a b , . . .

+3

All Articles