The difference between weak and soft references in Java and the introduction of virtual and strong references _java

Source: Internet
Author: User
Tags garbage collection

Knowing the concepts of weak references and soft references are different from how they are used, and referencing classes play an important role in the process of garbage collection. We all know that the garbage collector reclaims the memory of objects that match the recycle criteria, but not all programmers know that the recycle condition depends on the type of reference to the object. This is the main difference between weak and soft references in Java. If an object has only a weak reference pointing to it, the garbage collector immediately reclaims the object, which is an urgent way to reclaim it. In contrast, if a soft reference points to these objects, the objects are recycled only if the JVM requires memory. The special behavior of weak references and soft references makes them useful in some situations. For example, soft references are good for caching, and when the JVM needs memory, the garbage collector reclaims the objects that are only referenced by soft references. Weak references are ideal for storing meta data, such as storing classloader references. If no class is loaded, then there is no reference to ClassLoader. Once the last strong reference is removed, only weakly referenced classloader are reclaimed. In this article we will cover different types of Java references, such as strong references (strong Reference) and virtual references (phantomreference).

Weak reference vs Soft reference in Java

There are four types of references in Java:

1. Strong references (strong Reference)
2. Weak reference (WeakReference)
3. Soft Reference (SoftReference)
4. Virtual Reference (Phantomreference)

Strong references are the simplest references we use in programming, such as code string s= "ABC" in which variable S is a strong reference to the string object "ABC". Any object that is pointed to by a strong reference cannot be reclaimed by the garbage collector, and these objects are required in the program. Weak references are represented by Java.lang.ref.WeakReference class classes, and you can create weak references using the following code:

Copy Code code as follows:

Counter Counter = new Counter (); Strong Reference-line 1
weakreference<counter> weakcounter = new weakreference<counter> (Counter); Weak reference
counter = NULL; Now Counter object are eligible for garbage collection

Now, as long as you give the strong reference object counter NULL, the object can be reclaimed by the garbage collector. Because the object no longer contains any other strong references, even a weak reference to the object Weakcounter cannot prevent the garbage collector from reclaiming the object. Conversely, if the object contains a soft reference, the Counter object will not be reclaimed immediately unless the JVM requires memory. Soft references in Java are represented using the Java.lang.ref.SoftReference class, and you can create soft references using the following code:

Copy Code code as follows:

Counter prime = new Counter (); Prime holds a strong Reference–line 2
SoftReference soft = new SoftReference (prime); Soft reference variable has softreference to Counter Object created in line 2

Prime = null; Now Counter object is eligible to garbage collection but only to collected when JVM absolutely needs

After a strong reference is empty, the second behavior object of the code counter creates a soft reference that also does not prevent the garbage collector from reclaiming objects, but can defer the collection, unlike the objects that are eagerly reclaimed in the weak reference. Given the difference between soft and weak references, soft references are more appropriate for caching mechanisms, and weak references are more useful for storing metadata. Another example of using a weak reference is Weakhashmap, which is another implementation of the map interface in addition to HashMap and TreeMap. Weakhashmap has a feature: the Key values (keys) in the map are encapsulated into weak references, which means that once a strong reference is deleted, a weak reference within the WEAKHASHMAP cannot prevent the object from being reclaimed by the garbage collector.

A virtual reference is the third available reference in the JAVA.LANG.REF package package, expressed using the Java.lang.ref.PhantomReference class. Objects that have virtual references can be reclaimed by the garbage collector at any time. Like weak references and soft references, you can create a virtual reference by using the following code:

Copy Code code as follows:

Digitalcounter digit = new Digitalcounter (); Digit reference variable has strong Reference–line 3
Phantomreference Phantom = new phantomreference (digit); Phantom reference to object created in line 3

digit = NULL;

Once a strong reference is removed, the Digitalcounter object in the third row can be reclaimed by the garbage collector at any time. Because only one virtual reference points to the object, the virtual reference cannot prevent the garbage collector from reclaiming the object.

In addition to understanding weak references, soft references, virtual references, and weakhashmap, you need to understand referencequeue. In the process of creating any weak references, soft references, and virtual references, you can provide reference queue Referencequeue in the following code:

Copy Code code as follows:

Referencequeue refqueue = new Referencequeue (); Reference'll be stored in the this queue for cleanup
Digitalcounter digit = new Digitalcounter ();
phantomreference<digitalcounter> Phantom = new phantomreference<digitalcounter> (digit, refQueue);

The reference instance is added to the reference queue, and you can then recycle the object by querying the queue at any time. The life cycle of an object can be described in the following illustration:


This is the difference between weak and soft references in Java. We also learned some basic reference classes: Weak references, soft references, virtual references, and Weakhashmap and Weakhashmap. In summary, reasonable use of references can help the garbage collector better manage Java memory.

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.