jvm reference

After the JDK1.2, the referenced object into four states, i.e. strong references, soft, weak, and an imaginary reference cited. In this way, you can be more flexible control over the life cycle of the object.
 
First, the strong reference
the most is the strong references used in the development.

A strong reference statement format:

String str = "abc";
as long as an object associated with strong references, the JVM under low memory conditions, preferring to throw outOfMemoryError wrong, it will not recover such objects.

 

[1] If we want to reclaim JVM such objects are associated with strong references, we can show to be set str is null, then the JVM will recover this object at the right time.

 

Second, the soft references
java SoftRefence used to represent the soft references, if an object is associated with soft references, the JVM will only recover the object under low memory conditions.

So take advantage of this feature, soft references can be used for caching.

Soft references statement format:

import java.lang.ref.SoftReference;
 
public class TestRef {
    public static void main(String args[]) {
        SoftReference<String> str = new SoftReference<String>(new String("abc"));
        System.out.println(str.get());
        //通知JVM进行内存回收
        System.gc();
        System.out.println(str.get());
    }
}
输出:

Can see that, at this time sufficient JVM memory, not yet recovered soft reference object is associated.

[1] suitable for soft reference cache when sufficient memory reference values ​​directly through the soft, without querying data from real sources, can significantly improve the website performance. When out of memory, allowing the JVM garbage collection, deleting cache, this time only from the true source query data.

 

 

Third, weak references
 

java WeakReference used to represent weak. If an object is associated with a weak reference, then when the JVM garbage collection is performed, regardless of whether sufficient memory, such objects will be recovered.

Weak references statement format:

import java.lang.ref.WeakReference;
 
public class TestRef {
    public static void main(String args[]) {
        WeakReference<String> str = new WeakReference<String>(new String("abc"));
        System.out.println(str.get());
        //通知JVM进行内存回收
        System.gc();
        System.out.println(str.get());
    }
}
输出:

Can see, is associated with a weak reference object will always be recovered out when garbage collection.

Note: (1) System.out.println (str.get ()); it is possible to take a value of less than str. This is because after we declare a weak reference, its output immediately, and gc thread is a very low priority thread guard, had a chance to scan the area where the object, that is, too late to recover the object. If we are after a very long time after the statement, once again we get, it is possible to get less value.

 

[1] weak references may be in the callback function to prevent memory leaks. Because the callback function is often anonymous inner classes, a non-static inner classes will be held implicitly a strong reference to the outer class when JVM at the time of recovery outside the class, this time in a thread inside the callback function is callback , JVM will not be collected outside class, resulting in a memory leak. Andrews activity is in the declaration of a non-static inner classes, considering prevent memory leaks, it should explicitly declare this inner class holds a weak reference the outer class.

 

Fourth, the virtual reference
java PhantomReference used to represent a virtual reference. False references, virtual references, cited as useless as no reference as an object associated with the same. If an object is associated with a virtual reference, then at any time JVM could be recovered off. Virtual reference can not be used alone, it must be used together with the reference queue.

Virtual reference declaration format:

import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
 
 
public class TestRef {
    public static void main(String args[]) {
        ReferenceQueue<String> queue = new ReferenceQueue<>();
        PhantomReference<String> str = new PhantomReference<String>("abc", queue);
        System.out.println(str.get());
 
    }
}
输出:

Visible using the get method can not obtain the object's value

 

[1] When the garbage collector is ready recovery of an object, if it is found associated with the virtual reference, it will prior to recovery, this reference is added to the reference virtual queue. The program can determine whether the reference queue has joined the phantom reference, to see whether the referenced object to be recovered, if you really want to be recovered, you can do some finishing work before recycling.
 

Original: https://blog.csdn.net/qq_41723615/category_9704605.html

Published 740 original articles · won praise 65 · Views 100,000 +

Guess you like

Origin blog.csdn.net/qq_41723615/article/details/104342675
JVM
JVM