Types of Java References

Source: Internet
Author: User

---restore content starts---

  From the first article seems to have a long time, MD procrastination is more and more serious, but 10 o ' clock every day to get home from work more than 11, just want to sleep. Not blind BB, go straight to work. Mainly want to record about Java reference.

Java should be used to refer to the word very very familiar with it, remember the beginning of Java, the word has been accompanied by us. Another very important thing in Java is the GC, for GC, the flag that determines whether an object can be recycled is that the object is still being referenced (about the next time the GC specifically records a wave), the JVM for better management of object references, There are three classes under the Lang.ref package: Softreference,phantomreference,weakreference. Represents soft references, virtual references, and weak references, respectively.

So in summary, does Java have only these three kinds of references? The answer is no, it's 4 kinds. And a strong reference to use every day. It may be that many people keep new objects every day without knowing that they are called strong references. Here are a brief introduction of these four references

Strong quote: We keep new objects every day, is the most common way of quoting, for example, person p = new person (); Then P is a strong reference variable. Because the JVM does not reclaim strongly referenced objects, strong references are one of the main reasons why Java causes memory leaks.

Soft reference: is implemented through the SoftReference, if an object has only soft references, he may be collected by GC (note the word, it is possible), when the system's memory space is sufficient, he will not be recycled, when the space is not enough, it would be recycled.

Weak references: Weak references and soft references are somewhat similar, the main difference is that the period of the weak reference is shorter, the reference level is lower, with weakreference implementation, as long as the GC occurs, regardless of memory enough, the weak reference will be GC off.

Virtual reference: Soft references and weak references can be used alone, but the virtual reference can not, its main function is to track the state of garbage collection, the program can be associated with check and virtual reference queue is not a one containing the specified virtual reference, so as to understand that the referenced object is not recycled. The system is not able to obtain the referenced object by means of a virtual reference. Personally feel that the meaning of virtual reference is not very big, understand there is such a thing can.

  

Types 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.