Strong references, soft references, weak references, the difference between virtual references and their effects

Source: Internet
Author: User
Object references in the Java language fall into the following categories: Strong references, soft references, weak references, virtual references

Strong reference is usually the most commonly used reference, when the memory space is low, the Java virtual machine would rather throw a outofmemoryerror error, so that the program terminates abnormally, also will not be free to recycle strong reference objects to solve the problem of insufficient memory.

If an object has only a soft reference, the memory space is sufficient, the garbage collector will not recycle it, and if the memory space is low, the memory of those objects is reclaimed.

Objects that have only weak references have a more ephemeral lifecycle. When the garbage collector thread scans the area of memory it governs, it reclaims its memory whenever it discovers an object that has only a weak reference, regardless of whether the current memory space is sufficient or not.

Virtual reference, this reference is not commonly used, the main purpose is the result of referencing the associated object, to implement the object reference relationship tracking. A virtual reference does not determine the life cycle of an object. If an object holds only a virtual reference, it is likely to be reclaimed by the garbage collector at any time, as without any reference.

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.