Java Virtual machines: How can I determine which objects are recyclable?

Source: Internet
Author: User

Copyright NOTICE: This article for Bo Master original article, reproduced please indicate the source, Welcome to exchange Study!

In heap memory, where almost all of the object instances in the Java program are stored, the capacity of the heap memory is limited, and the Java virtual Opportunity manages the heap memory to reclaim the "dead" objects (that is, objects that cannot be used by any means), freeing up memory. The first thing to do before the garbage collector reclaims heap memory is to determine which of these objects are still alive and which are dead. How does a Java virtual machine determine if an object can be recycled?

Reference Counting Algorithm

The principle of the reference counting algorithm is this: Add a reference counter to the object, and whenever there is a place to reference it, the counter value is incremented by 1, and when the reference fails, the counter value is reduced by 1, and at any moment the object with a value of 0 is impossible to reuse, that is, the object that can be recycled.

The reference counting algorithm is highly efficient, but the mainstream JVM does not use this algorithm to determine the recyclable object because it has a fatal flaw that it cannot solve the problem of circular referencing between objects, and it cannot be recycled for objects that are referenced by loops.

Suppose there is a code like this:

 Public classObject { PublicObject instance;  Public Static voidMain (string[] args) {//1Object objecta =NewObject (); Object OBJECTB=NewObject (); //2Objecta.instance =OBJECTB; Objectb.instance=objecta; //3Objecta =NULL; OBJECTB=NULL; }

After the program is started, Objecta and OBJECTB two objects are created and allocated memory in the heap, both of which hold each other's references, in addition, there are no other references to these two objects, in fact, these two objects are no longer accessible (the reference is empty, inaccessible), However, because they reference each other, causing their reference counters to be 0, the reference counting algorithm cannot tell the GC collector to reclaim them.

In fact, when step 1th executes, the reference counter value for two objects is 1, and when step 2nd executes, the reference counter for two objects is 2, and when step 3rd executes, both are cleared to null and the reference counter value becomes 1. According to the idea of the reference counting algorithm, objects with values not 0 are considered to be alive and will not be recycled, but in fact the two objects are no longer accessible and should be recycled.

Accessibility Analysis Algorithm

in the mainstream JVM implementations, the accessibility analysis algorithm is used to determine whether an object survives. The basic idea of the accessibility analysis algorithm is that through a series of objects called "GC Roots" as the starting point, starting from these nodes, searching through the path called the reference chain, when an object to the GC Roots object does not have any reference chain connected, it is considered that the GC Roots to this object is unreachable , this object is determined to be unavailable and can be recycled.

In, Objecta, OBJECTB, OBJECTC are accessible, will not be recycled, OBJECTD, objecte Although there are associations, but they to the GC roots is unreachable, so they will be judged to be recyclable objects.

In Java, the objects that can be used as GC roots include the following:

1, the object referenced in the virtual machine stack;

2. The object referenced by the class static property in the method area;

3, the object in the method area constant reference;

4. The object referenced by the native method in the local method stack.

The above discusses the two algorithms to determine whether the object can be recycled, whether the object can be recycled is only the first step of garbage collection, and then to solve the problem of when and how to recycle, in the following article we will explore these issues.

Java Virtual machines: How can I determine which objects are recyclable?

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.