Four ways of referencing in Java (strong references, soft references, weak references, virtual references) __java

Source: Internet
Author: User

Original address: http://www.cnblogs.com/alias-blog/p/5793108.html Strong reference (Strongreference)

Strong references are common in program code, such as the object and STR in the following code are strong references:

Object = new Object ();
String str = "Hello";
as long as an object has a strong reference associated with it, the JVM must not recycle the object, even if there is not enough memory, the JVM would rather throw a outofmemory error than recycle the object. For example, the following code:

public class Main {public
    static void Main (string[] args) {
        new Main (). FUN1 ();
    }

    public void Fun1 () {
        object = new Object ();
        object[] Objarr = new object[1000];
    }

When running to object[] Objarr = new object[1000]; if there is not enough memory, the JVM throws Oom error and does not reclaim objects that object points to. Note, however, that when the fun1 is finished, both object and Objarr no longer exist, so the objects they point to are recycled by the JVM.

If you want to break the association between a strong reference and an object, you can show that the reference is assigned null, so that the JVM reclaims the object at the right time. Soft Reference (SoftReference)

Soft references are used to describe some useful but not necessary objects, expressed in Java by the Java.lang.ref.SoftReference class. For objects associated with a soft reference, the JVM reclaims the object only when there is not enough memory . Therefore, this can be a good solution to the problem of oom, and this feature is suitable for caching: such as Web caching, image caching, and so on.

Soft references can be used in conjunction with a reference queue (Referencequeue), and a soft reference is added to the associated reference queue if the object referenced by the soft reference is reclaimed by the JVM. The following is a sample usage:

Import java.lang.ref.WeakReference;

public class Main {public
    static void Main (string[] args) {

        weakreference<string> sr = new Weakreference< ; String> (New String ("Hello"));

        System.out.println (Sr.get ());
        System.GC ();                Notifies the JVM of GC for garbage collection
        System.out.println (Sr.get ());
    }
Weak reference (WeakReference)

Weak references are also used to describe non-essential objects, and objects associated with weak references are reclaimed when the JVM is garbage collected, regardless of whether memory is sufficient . In Java, this is represented by the Java.lang.ref.WeakReference class. The following is a sample usage:

Import java.lang.ref.WeakReference;

public class Main {public
    static void Main (string[] args) {

        weakreference<string> sr = new Weakreference< ; String> (New String ("Hello"));

        System.out.println (Sr.get ());
        System.GC ();                Notifies the JVM of GC for garbage collection
        System.out.println (Sr.get ());
    }
Virtual Reference (phantomreference)

A virtual reference differs from a previous soft reference or weak reference, and does not affect the object's lifecycle. Represented in Java with the Java.lang.ref.PhantomReference class. If an object is associated with a virtual reference, it is likely to be reclaimed by the garbage collector at any time, as is the case with no reference to it.

Note that a virtual reference must be associated with a reference queue, and when the garbage collector is ready to recycle an object, if it finds a virtual reference, it adds the virtual reference to the reference queue associated with it. A program can determine whether a referenced object will be garbage collected by determining whether a virtual reference has been added to the reference queue. If the program finds that a virtual reference has been added to the reference queue, you can take the necessary action before the memory of the referenced object is reclaimed. further understanding soft references and weak references

For strong references, we usually use them when writing code. For the other three types of references, the most commonly used are soft and weak references, and these 2 kinds have similarities and differences. They are all used to describe non-essential objects, but objects associated with soft references are reclaimed only when there is not enough memory, and objects associated with weak references are always reclaimed when the JVM is garbage collected . For the above features, Soft references are suitable for caching, allowing the JVM to reclaim memory when there is not enough memory, and weak references can be used to prevent memory leaks in callback functions . Because the callback function is often an anonymous inner class that implicitly holds a reference to an external class, if the callback function is called back in another thread, then if the external classes need to be recycled, the memory leaks because the anonymous inner class holds strong references to external classes.

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.