Java Object Recycling Process

Source: Internet
Author: User

I. Recoverable object judgment

Reference counter algorithm: Place a reference counter for an object, add a counter when the object is referenced, or, if the counter of an object is identified as zero, that object can be recycled. This is a simpler approach, but it is not possible to resolve the case where objects are referenced to each other.

GC Roots algorithm: This method can solve the problem that the object in the above algorithm cannot judge the recoverable state by reference to each other. As the name implies, GC root refers to a program using objects, there will always be a "portal", which can be a local variable reference in the stack, it can also be a class static property reference in the method area, a constant reference, a JNI Java reference. When an object is not available to all GC roots along the object reference chain, this proves that the object is not accessible to the GC roots, which is the "ingress" that the object does not use, and is naturally recyclable.

(Figure 1:GC Roots algorithm)

1, when the Root3=null, OBJECT5, 6, 7 to all root unreachable, even if OBJECT6 and Object7 are mutually referential relationship can also be judged to be recyclable status. If you want to reclaim Object2, you need the reference chain for that object to ROOT1 and Root2 to be broken before you can reclaim it.

Second, the process of object recycling

(Figure 2:java Object recycling Flowchart)The object recycling process, as shown, two points to note are:1, an object GC roots is not reachable and has implemented a finalize method can be recycled;2. The Finalize method of an object will only be executed once;

Java Object Recycling Process

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.