The JVM (3) 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 by the root algorithm, root algorithm, what is called Root, that is, static members of the class, static members is the kind of static decoration, is the "root" of the one, the root also contains the member variables in the method. Just because a member or object is not hanging on the root, the GC may get rid of them, the circular reference mentioned here, depends on whether the circular reference hangs on the root, assuming it hangs on the root. Assuming that the root is still being run by the JVM's Java code, it will not be GC-out, assuming that the root has been released. This object is not hanging up. The object will be dropped by GC.


② said that the root search algorithm, ROOTS, the algorithm, those in Java will be considered to be the root of, in my impression is usually static decorated class members, such as static fields, such fields refer to the object is called the root. Only the class is not unloaded in the pool area. Always in the heap, the class object is simply not recycled, and the object he refers to will not be GC.


③ Besides, there is another situation. In the stack of methods, the stack has his stack member Integer a = XXX, when the method is not released. When there is no stack, the method is not ejected, then the object referenced by integer A is not recycled, under what circumstances, it is that the object is not hanging on the root. will be recycled.


④ we go back to the question of the title, whether this circular reference is recycled, just see if the circular reference hangs on the root, a references to B,b reference A, A and B are not hung on some memory element and root, when their life cycle ends. Both of these objects are likely to be recycled.


⑥ The mechanism of detailed recycling, it is more complicated. Each time the GC. Mark the object to be recycled once. For example, there will be a counter every time +1,+1,+1. 11 times per GC. When the object reaches the default value. Let's say it seems like 15 times, when the new band created 15 times, it will be reached in the old age band, and the recovery frequency of the older objects is not the same as the frequency of the recovery of the new belt, which can be carefully considered in the pool partition. Understand their enforcement mechanisms.




Note: JVM heap chunks

Generation-Yonggeneration/newgeneration: The New generation. The surviving object in the EDEN/S0/S1.

-Oldgeneration: the old age. The surviving object in the tenured area.
-Permanentgeneration: Permanent generation.
Space Area-Eden: Eden area. is a region of the Cenozoic.


-Survivor: Survival zone, belongs to the new generation, 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

The JVM (3) 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.