4 kinds of references and GC Roots in java

1. First, the four citations are as follows:

  • FinalReference strong reference
  • SoftReference soft reference
  • WeakReference weak reference
  • PhantomReference Phantom Reference

2. Features of the four citations:

Strong references : all references to objects that are new are strong references

              eg:Student s = new Student();

               Recycling timing: It will not be recycled, and memory overflow will occur.

Soft references : When the memory associated with soft references is insufficient, the objects associated with these soft references will be included in the scope of garbage collection and then recycled, which means that soft references are not completely safe. In this case, it will be collected by the garbage collector.

public static void main(String[] args) {

    SoftReference[] references = new SoftReference[5];

    ReferenceQueue<ReferenceTestObject> referenceTestObjectReferenceQueue = new ReferenceQueue<>();

    for(int i =0 ;i<5;i++){

        references[i] = new SoftReference(new ReferenceTestObject("ahahh-"+i),referenceTestObjectReferenceQueue);

    }



    for(int i =0 ;i<5;i++){

        Object o = references[i].get();

        if(o == null){

            System.out.println("null");

        }else{

            System.out.println(((ReferenceTestObject)o).name);

        }

    }

}

Usage scenario: Use soft references to save the data taken from the database, specifically encapsulating a middle layer. The function of the middle layer is to judge whether the data is null when getting the data, and if it is null again Read from the database, and then put it into the collection of soft references. This approach can avoid memory overflow .

Weak reference : as long as GC occurs, it will be recycled

eg:ThreadLocalMap的key

 static class ThreadLocalMap {

        /**
         * The entries in this hash map extend WeakReference, using
         * its main ref field as the key (which is always a
         * ThreadLocal object).  Note that null keys (i.e. entry.get()
         * == null) mean that the key is no longer referenced, so the
         * entry can be expunged from table.  Such entries are referred to
         * as "stale entries" in the code that follows.
         */
        static class Entry extends WeakReference<ThreadLocal<?>> {
            /** The value associated with this ThreadLocal. */
            Object value;

            Entry(ThreadLocal<?> k, Object v) {
                super(k);
                value = v;
            }
        }
    ......}

phantom reference

There is the same thing as there is no: you can't get an instance of an object through a virtual reference, but it still works. , The function is to receive a system notification when the object is recycled by the collector, and to track the garbage collector's recycling action. For example, when the object is recycled, the finalize method of the object will be called.

ReferenceQueue reference queue :

When creating a Reference, manually register the Queue in the Reference, and when the object referenced by the Reference is reclaimed by the garbage collector, the JVM will put the Reference in the queue, and we can do other business with the queue. , which is equivalent to a notification mechanism.

Accessibility Analysis:

How does the JVM know whether the reference is present? This involves the JVM's reachability analysis algorithm. The simple idea of ​​the JVM's reachability analysis algorithm is to use a series of GC Roots as the starting point to search downwards, and the path followed by the search is called Reference chain, when an object does not have any reference chain to GC Roots, which means that the object is unreachable from GC Roots, it proves that the object is unavailable and can be recycled. As shown below

Objects 4, 5, and 6 are all recyclable. So the question is, which objects can be used as GC Roots? Here are a few, as follows

  • Objects referenced in the virtual machine stack

  • The object referenced by the static property of the class in the method area

  • The object referenced by the constant in the method area

  • Objects referenced by the native method stack JNI

     

Guess you like

Origin blog.csdn.net/ahou2468/article/details/122330008