Java Object Circular Reference, how the Java GC reclaims

Source: Internet
Author: User
Tags garbage collection
Students who have used smart pointers in C + + should be aware of the great convenience that smart pointers can bring to memory management in C + +, but also introduce headaches, such as circular references brought by smart pointers, that have not been well resolved in previous projects.         recently participated in the development of Android projects, the Java Memory management has a preliminary understanding, it is easy to think of the problem of circular reference. For example, the following example:         public void Buiddog ()         {        &NBS P  dog Newdog = new Dog ();           Tail newtail = new Tail ();           newdog.tail = Newtail;           Newtail.dog = Newdog;        }         in here, Newtail is holding a reference to Newdog, Newdog is holding a reference to Newtail. If the newdog is to be reclaimed, the premise is that Newtail is reclaimed before the reference to Newdog can be released. But back to the Newtail, the premise is to be recycled newdog to be first recycled. When the Builddog function exits, it seems that garbage collection management will never be able to recycle the two objects that are actually no longer needed.           Whether the garbage collection mechanism can solve the dilemma of circular reference, with this query to find some information, found a more satisfactory explanation. In Appendix A of the book "Java Platform performance:strategies and Tactics," one of the instructions is that the book comes from Employees of the Sun company Java team and should be considered more authoritative. There is such a paragraph (http://java.sun.com/docs/books/performance/1st_edition/html/JPAppGC.fm.html#997428):     "It's important to", that is not just any strong reference would hold an object in memory.  &nbs P   These must to references that chain from a garbage collection root.      GC roots are a special clas s of variable that includes  temporary variables on the stack (of any thread)         Static Vari Ables (from no class)         Special references from JNI native code ". This passage can be simply understood that strong references do not guarantee that objects are not recycled. In addition to checking whether an object is referenced, the garbage collection mechanism also depends on whether the object is referenced directly or indirectly by at least one GC roots object. GC roots objects include the following class capacity: 1 The current function call stack for each thread, from the stack top to the bottom of the stack in each function of the local variables. 2 static variable 3 is referenced to in JNI.         Therefore, the garbage collection mechanism can discover this problem, although there is a strong reference to the two loops referenced in the above example, but not directly or indirectly referenced by any GC root object.         Also, to verify this, look at the code for GC management in the Android source. In the marksweep.c file, there is a void Dvmheapmarkrootset () function, which has some detailed descriptions of the GC root object and is interesting to look at.         So Java has its own solution for circular references. But then again, the general actual coding in the circular reference is not as obvious as the above example, is generally a complex reference to multiple objects caused by the loop, this time, if an object's life cycle is very long, it will cause multiple objectsCan not be released, so you should pay special attention to the reference relationship between objects. Reproduced in: http://blog.sina.com.cn/s/blog_6a0eedb60100y76b.html

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.