Summary of Java References

Source: Internet
Author: User

four kinds of references : soft and weak

Strong references: Using strong references, the garbage processor will not reclaim him when it is out of memory, even if it causes the program to crash for example: a a=new A ()

Soft references: When memory is low, it is recycled (a soft reference can be used in conjunction with a reference queue (Referencequeue), and if the object referenced by the soft reference is reclaimed by the garbage collector, the Java Virtual machine adds the soft reference to the reference queue associated with it. ) ———— A cache that is typically used to build sensitive information

Weak reference: As long as the garbage processing to scan to have weak reference objects, insufficient memory to be recycled (can be used in conjunction with a reference queue (Referencequeue), if the soft reference object referenced by the garbage collector is reclaimed, the Java Virtual machine will add the soft reference to the reference queue associated with it. )

Virtual reference: will be recycled at any time, in a fake form (and must be used in conjunction with the reference queue)

Weak references : (non-required objects)

Import Java.lang.ref. WeakReference;  Public classMain { Public Static voidMain (string[] args) {WeakReference<String> sr =NewWeakreference<string> (NewString ("Hello")); System. out. println (Sr.Get());                System.GC (); //notifies the JVM of GC for garbage collectionSystem. out. println (Sr.Get()); }}

Output Hello null

Use of soft references:

The SoftReference feature is that an instance of it holds a soft reference to a Java object, and the existence of the soft reference does not prevent the garbage collection thread from reclaiming the Java object. That is, once SoftReference has saved a soft reference to a Java object, the Get () method provided by the SoftReference class returns a strong reference to the Java object before the garbage thread recycles the Java object. Also, once a garbage thread reclaims the Java object, the Get () method returns NULL. Look at the following code:

MyObject aRef = new MyObject ();

SoftReference asoftref=new softreference (AREF);
At this point, for this MyObject object, there are two reference paths, one is a soft reference from the SoftReference object, and one is a strong reference from the variable areference, so this MyObject object is a strong object. We can then end Areference's strong reference to this MyObject instance:

ARef = null;

Since then, this MyObject object has become a soft and accessible object. If the garbage collection thread is in memory garbage collection, it does not always retain the object because it has a softreference reference to the object. The Java Virtual machine's garbage collection thread treats soft-and object-and other generic Java objects differently: The cleanup of soft and object is determined by the garbage collection thread according to its memory requirements based on its particular algorithm. That is, the garbage collection thread reclaims the soft objects before the virtual machine throws OutOfMemoryError, and the virtual machine takes priority to reclaim the soft objects that are unused for long periods of time, and the "new" soft-anti-objects that have just been built or used are retained by the VM as much as possible. Before reclaiming these objects, we can pass: Myobject anotherref= (MyObject) asoftref.get (); &NBSP; the
re-obtains a strong reference to the instance. After the collection, the call to get () method can only get null. 3 using Referencequeue to clear the softreference that lost the soft reference object as a Java object, the SoftReference object has a generic Java object in addition to the particularity of saving soft references. So, when a soft and an object is recycled, although the get () method of the SoftReference object returns NULL, the SoftReference object no longer has the value of being present and requires an appropriate cleanup mechanism. Avoid memory leaks caused by a large number of SoftReference objects. The Referencequeue is also available in the Java.lang.ref package. If you are creating a SoftReference object, use a Referencequeue object as the constructor for the softreference, such as:

Referencequeue Queue = new referencequeue ();


SoftReference ref=new softreference (amyobject, queue);

Then when the SoftReference soft-referenced amyohject is reclaimed by the garbage collector, ref's strongly referenced SoftReference object is included in the Referencequeue. That is, the object saved in Referencequeue is the reference object, and it is the reference object that has lost the object it is soft referenced. Also from the name of Referencequeue, it is a queue, when we call its poll () method, if this queue is not empty queue, then will return the queue in front of the reference object.
At any time, we can call Referencequeue's poll () method to check if there are non-strong objects that it cares about being recycled. If the queue is empty, a null is returned, otherwise the method returns a reference object in front of the queue. Using this method, we can check which softreference the soft reference object has been recycled. So we can get rid of the SoftReference objects that have lost the soft reference object. The usual ways are:

SoftReference ref = null;

while (ref = (EMPLOYEEREF) q.poll ()) = null) {

Clear Ref

}

Summary of Java References

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.