Strong and weak references in Java

Source: Internet
Author: User

Definition:

1. Strong reference
General references are actually strongly referenced, which is the most common reference. If an object has a strong reference, it is similar to an essential necessities. The Garbage Collector will never recycle it. When the memory space is insufficient, the Java Virtual Machine would rather throw an outofmemoryerrorProgramIf an exception is terminated, the memory insufficiency problem will not be solved by recycling strongly referenced objects at will.

2. Soft reference)

If an object only has soft references, it is similar to the necessities that can be used. If the memory space is sufficient, the garbage collector will not recycle it. If the memory space is insufficient, the memory of these objects will be recycled. The object can be used by programs as long as the garbage collector does not recycle it. Soft references can be used to implement memory-sensitive high-speed cache.
Soft references can be used together with a referencequeue, the Java virtual machine adds this soft reference to the reference queue associated with it.

3. weakreference)
If an object only has weak references, it is similar to a necessities that can be used. The difference between weak references and soft references is that only objects with weak references have a shorter life cycle. When the Garbage Collector thread scans the memory area under its jurisdiction, its memory will be recycled no matter whether the current memory space is sufficient or not. However, since the garbage collector is a thread with a low priority, it may not soon find objects with weak references.
Weak references can be used together with a referencequeue, the Java virtual machine adds this weak reference to the reference queue associated with it.

4. phantomreference)
As the name implies, "virtual references" are just the same as virtual ones, which are different from other types of references. Virtual references do not determine the object lifecycle. If an object only holds a virtual reference, it is the same as no reference and may be recycled at any time.
Virtual references are mainly used to track the activity of objects being reclaimed by garbage collection. A difference between virtual and soft references and weak references is that virtual references must be used together with the reference Queue (referencequeue. When the zookeeper recycler is about to recycle an object, if it finds that it has a virtual reference, it will add this virtual reference to the reference queue associated with it before it recycles the object's memory. The program can know whether the referenced queue is added with virtual references.

Whether the referenced object is to be recycled. If the program finds that a virtual reference has been added to the reference queue, it can take necessary action before the memory of the referenced object is recycled.

 

 

Terms:

Stronugly reachable: objects that can be accessed through strong references.

Softly reachable: an object that is not highly accessible and can be accessed through soft references.Soft reference objects are most commonly used for memory-sensitive caching.

Weakly reachable: objects that are not strong or soft or accessible through weak references.Weak references are most commonly used for standardized ing..

Phantomly reachable.



Test

 Package Com. DDC. test;

Import Java. Lang. Ref. referencequeue;
Import Java. Lang. Ref. weakreference;

Public Class Referencetest {

/**
* @ Param ARGs
* @ Throws Interruptedexception
*/
Public Static Void Main (string [] ARGs) Throws Interruptedexception {
// Todo auto-generated method stub
// Create a strong reference
String strheap = New String ("hello ");
String strstatic = "test ";
// Creates a reference queue. The <string> parameter indicates the reference of the string object in the queue.
Referencequeue <string> RQ = New Referencequeue <string> ();
// Creates a weak reference that references the "hello" object and is associated with the RQ reference queue.
// Creates a weak reference for an object.
Weakreference <string> WF1 = New Weakreference <string> (strheap, rq );
Weakreference <string> WF2 = New Weakreference <string> (strstatic, rq );
Strheap = Null ; // ④ Cancel the strong reference of the "hello" Object
Strstatic = Null ;
System. Out. println (wf1.get () + "" + wf2.get ());
System. GC (); // ⑤
System. GC (); // ⑥
Thread. Sleep (5000l );
// It recycles objects in weak references, rather than weak references.
// The objects pointed to by weak references are recycled. This weak reference will be placed in referencequeue.
// Because strheap is a heap object (which will be recycled by GC), and strstatic String constant (which will not be recycled by GC), it is referenced in WF1 to be gc, and WF2 does not
System. Out. println (wf1.get () + "" + wf2.get ());
System. Out. println (RQ. Poll (). Get ());
Thread. Sleep (1000l );
System. Out. println (RQ. Poll (). Get ());


}

}

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.