Objects A and B are circular references, and will not be recycled by GC at the end? -------a GC mechanism for Java

Source: Internet
Author: User


① first of all, the GC inside the JVM is used in the root algorithm, the root algorithm, what is called the root, that is, the static members of the class, static members is the type static decoration, is the "root" of one, the root also includes the member variables in the method, only members or objects are not hanging on the root, GC when it is possible to get rid of them, referred to in the circular reference, see if the circular reference is hanging on the root, if the root is still executed by the Java code of the JVM, it will not be GC off, if the root has been released, this object is not hanging up, The object will be dropped by GC.


② said the root search algorithm, ROOTS, the algorithm, those in Java will be considered to be the root, in my mind is generally static modified class members, such as static fields, this field refers to the object is called the root, as long as the class is not unloaded in the pool area, has been in the heap, As long as the class object is not recycled, the object he references will not be GC.


③ say another situation, the stack in the method, the stack has his stack member Integer a = XXX, when the method is not released, there is no stack, the method is not ejected when the object referenced by the Integer A will not be recycled, under what circumstances, the object is not hanging on the root , it will be recycled.


④ we go back to the question of the title, whether this circular reference is recycled, whether the circular reference is hung on the root, a refers to b,b reference A, A and B are not hung on a memory element and root, and when their life cycle ends, both objects are likely to be recycled.


⑥ specific recovery mechanism, it is more complicated, each time GC, the object to be recycled tag once, for example, there will be a counter each time the +1,+1,+1, each time the GC 11 times, when the object reached the default value, for example, like 15 times it, In the new belt created by the object reached 15 times will be reached the old age belt, and the old age object recovery frequency and the recovery of the frequency is not the same, you can carefully look at the pool partition, understand their operating mechanism.




Note: JVM heap chunks

Generation-Yonggeneration/newgeneration: The new generation, the surviving object in Eden/s0/s1. -Oldgeneration: The old age, the object of survival in the tenured district.
-Permanentgeneration: Permanent generation.
Space Area-Eden: Eden area, a new generation of district.
-Survivor: Survival zone, belongs to the Cenozoic, in order to replicate the needs of the algorithm. Generally divided into two districts of equal size (S0/S1 or from/to). -Tenured: The area where the old age is stored.
-Permanent: Life zone.

: Hotspot's HEAP partition

Objects A and B are circular references, and will not be recycled by GC at the end? -------a GC mechanism for Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.