This is the 7th day of my participation in Gwen Challenge

Come to the seventh day of more text, is also the day of 6.7 college entrance examination, the students who are attending the college entrance examination, hope you test what what will, gold list title!

How can you program object-oriented without objects? What a bald head!

Before reading this article, take a look at section 4 of the link above to help us understand these four references!

This article introduces four Java references to prepare for the introduction of ThreadLocal! Java memory management is divided into memory allocation, and memory recovery, memory recovery is responsible by the garbage collector themselves, have special garbage collection, garbage collection and mainly to see if the object has a reference to the object, and the strength of the reference, are these four references: strong reference, soft references, weak references, and virtual reference!

1. Strong references

A strong reference is when you create an object and give it a reference, and this is a strong reference

   praivate static void strongReference(String[] args){
       Object o=new Object();
       // Do a lot of things
       // DO a lot of things
       
   }
    
Copy the code

O is a local variable that is automatically reclaimed by the JVM after the strongReference method ends, but is a strongReference that is out of Memory before the strongReference method ends, so the JVM does not reclaim o, raising an out of Memory exception. If you want to interrupt the reference, just set o to null, and then the instance of O will be collected by gc

2. Soft references

If an object has a soft reference, as long as there is enough memory, it will not be collected by GC, only when oom is about to occur, the GC will collect the object with a soft reference: oom is about to occur!

Objects with soft references:

GC: walk you!

GC takes away objects with soft references! Soft references can be used for memory sensitive caches, such as image caches, when memory is low images will not load, but not oom, so our code will be much better

public class TestDemo {

    static class User{}public static void main(String[] args) {
        User user=new User();// This is a strong reference
        SoftReference<User> userSoftReference=new SoftReference<>(user);/ / soft references
        user=null;
        System.out.println(userSoftReference.get());// Make sure the object is still there
        System.gc();// Do a garbage collection. Do not write this code into business code
        System.out.println(userSoftReference.get());
        List<byte[]> list=new LinkedList<>();
        try {
            for(;;) { list.add(new byte[1024*100]); }}catch (Throwable throwable){
            throwable.printStackTrace();
            // The User holding the soft reference has already left youSystem.out.println(userSoftReference.get()); }}}Copy the code

The results

3. Weak references

Weak references are weaker than soft references and only survive until the next GC for references to less important data

    public static void main(String[] args)  {
        User user=new User();// This is a strong reference
        WeakReference<User> userWeakReference=new WeakReference<>(user);/ / weak references
        user=null;
        System.out.println(userWeakReference.get());// Make sure the object is still there
        System.gc();// Do a garbage collection. Do not write this code into business code
        System.out.println(userWeakReference.get());
       
    }
Copy the code

Running results:

Weak references are used in the same way as soft references, except that weak references have a shorter lifetime, weak references, Handler memory leaks, and ThreadLocalMap entries in ThreadLocal

They are used to avoid memory leaks

4. Virtual reference PhantomReference

A virtual reference, also known as a ghost reference, can be reclaimed at any time and must be used with the ReferenceQueue

  public static void main(String[] args) throws FileNotFoundException {
        ReferenceQueue<String> referenceQueue=new ReferenceQueue<>();
        PhantomReference<String> phantomReference=new PhantomReference<>(new String("aa"),referenceQueue);
        System.out.println(phantomReference.get());
        
        }
Copy the code

Running results:

This virtual reference is rarely used in our development. Its purpose is to listen to whether the garbage collector is working properly!

5, summary

Four references, it represents the priority of the garbage collection when strong reference if you don’t interrupt references will not be recovered, even if your oom, and soft references is not enough memory, garbage collector will recycle it, and as long as a weak reference in garbage collection, it will be recycled, virtual reference out as long as it can be recycled, states that the gc is in normal work, If it is not collected, there may be a problem with the garbage collector!