Simply talk about how to determine if an object is recyclable in Java

Source: Internet
Author: User

  Background

When it comes to Java, one of the most important features is that Java allocates memory to objects through new in the heap, without the need for the programmer to voluntarily release it, but automatically by the Java virtual machine. This is one of the main differences between Java and C + +, so how does a virtual machine implement automatic recycling?  What is its basic recovery algorithm? This essay does not introduce these ~ ~, cooked words to eat a mouthful of food, the road to go step-by-step, this essay is mainly about the premise of recycling: How to judge an object can be recycled.

  

A general understanding of how an object can be recycled in Java

  Before learning the "deep understanding of Java Virtual Machine," in Java to determine whether an object can be recycled method, I naturally think of the method by reference count can do, I think: The object itself has a reference counter, whenever there is a place to reference it, the counter value is added 1 When a reference fails (for example, the program leaves the scope of its reference object), the counter value is reduced by 1, and any object whose counter is zero at any moment is no longer used, and the memory is recycled when it is automatically reclaimed;

This approach is simple and efficient, so why mainstream Java virtual machines are not used in this way, the main reason is that it is difficult to solve the problem of mutual circular reference between objects, see the following simple code example:

public class Main {

Public Object instance = null;

private static final int _1MB = 1024 * 1024;

Allow objects to occupy a certain amount of memory space, triggering recycling
Private byte[] Bigsize = new byte[2 * _1MB];

public static void Testgc () {

Main Obja = new Main ();

Main OBJB = new Main ();
The instance object in Obja references the instance object reference in OBJB,OBJB Obja
Obja.instance = OBJB;
Objb.instance = Obja;

Set two objects to null
Obja = null;
OBJB = null;

System.GC ();
}
}
for the above situation, if the Java virtual machine uses the reference counting method to judge whether the object can be recycled, then obja, and OBJB will never get recycled!

back to the topic, what method does the Java virtual machine use to determine whether an object can be recycled?
Accessibility analysis algorithm:
  
as the algorithm name describes, the basic idea of this algorithm is that through a series of objects called "GC Root" as the starting point, starting from these nodes to search downward, the path of the search is called the reference chain, when an object to the GC root It is not possible to prove that this object is not available when no reference chain is connected (in the case of graph theory, from the root node to the object node); 1-1, objects 5, 6, and 7 are not accessible to GC Root, but they are considered recyclable objects. That these objects can be judged to have died;




                         
1-1: Accessibility analysis algorithm Determines whether objects can be recycled

  
After watching, there will be a problem, that is, how to select GC Roots:
The following objects are included:
1. The object referenced in the virtual machine stack (which is the local variable table in the Java function Body).
2. The object referenced by the class static property in the method area.
3. The object referenced by the constant in the method area.
4. The object referenced by the JNI (that is, the generally said native method) in the local method stack.
  

The above is a part of my knowledge of the automatic recovery of Java memory, a summary and share, there are a lot of knowledge points need to learn, such as how the storage structure of the method area, what type of data storage, how the virtual machine manages the memory area.
I hope this sharing can inspire you, have a harvest ~





Simply talk about how to determine if an object is recyclable in 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.