Four reference principles for Java objects

Source: Internet
Author: User

I, Introduction to Java object referencesjdk1.2 before, when the object does not point to its reference, this object will be collected by GC, but sometimes we need to get back to use, and without its reference, can only rebuild an object, if optimized to do hashmap to cache to memory, but if the object construction consumes high or a lot of time, the memory O Om. After jdk1.2, Java has divided the object's references into 4 levels, allowing the program to flexibly control the life cycle of the object:1) Strong references. Strong citationuse is most common, when an object has a strong reference, it will not be collected by GC, whether the memory is sufficient or not enough. 2) Soft Reference. SoftReference class, the class contains a reference to the specified object, when an object is only soft-referenced and out of memory, the object will be collected by GC, can be used in conjunction with the Referencequeue queue, if the object is recycled, The reference to the specified object within the SoftReference will be set to the Null,softreference object to enter Referencequeue, using the poll method for Referencequeue to determine if the specified object is being reclaimed by GC.
3) Weak references. WeakReference class, the class contains a reference to the specified object, when an object has only a weak reference, whether the memory is sufficient or insufficient, will be GC recovery,can be used in conjunction with the Referencequeue queue,by using the poll method on the Referencequeue to determine whether the specified object is being reclaimed by GC, the first collection is marked as garbage collection, the object is obtained with GET, the second GC is reclaimed, and a get is returned empty. The weak reference is added to the reference queue when the pointing object becomes weak and the reference is reachable. This occurs before the object is refactored or the garbage collection actually occurs, and the reference to the object can be re-added to the non-canonical finalize method and the Resurrection object. 4) Virtual Reference. The Phantomreference class, when an object has only a weak reference, is recycled by GC at any time. Get is always empty through get. Virtual references are used to determine and track whether objects are recycled and must be used in conjunction with Referencequeue. Virtual references can avoid the problems caused by many of the destructor methods. Overriding the destructor, the GC marks it as garbage for the first time because the thread priority of the call destructor is very low, so it is not known how many GC cycles are required to reclaim the object, which is prone to memory leaks, and using virtual references to determine whether an object is actually being recycled by GC to avoid this problem.
II, Java Reference Scenario 1) because of the nature of the soft reference, when the memory is not enough to recycle only soft reference to the object, it is suitable for caching, when the user to access some data, from the database to pull back data, the establishment of soft references will cache those frequently queried data (rarely change, such as news, articles, etc.) , it gets from the cache when the user accesses it again and no longer gets it from the database.
2) Weak references
One scenario is when an application implements a custom ClassLoader, and when no object points to ClassLoader, the ClassLoader is recycled.

Null

Four reference principles for Java objects

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.