Strong references to Java objects, soft references, weak references, and virtual references

Source: Internet
Author: User

As we all know, Java is the JVM responsible for the allocation and recycling of memory, which is its advantage (easy to use, the program no longer like using C as the memory), but also its shortcomings (not flexible). In order to solve the problem that memory operation is not flexible, we can adopt soft reference method. In previous versions of JDK1.2, when an object was not referenced by any variable, the program could no longer use the object. That is, the program can only use the object if it is in a palpable state. This is like in daily life, after buying a certain item from the store, if it is useful, keep it, or throw it into the garbage bin and collect it from the cleaners. Generally speaking, if the item has been thrown into the dustbin, it is impossible to pick it up again.
But sometimes the situation is not so simple, you may encounter a similar chicken like the same items, food tasteless, discard. This kind of thing is useless now, keep it occupied space, but immediately throw it is not cost-effective, because it might come in handy in the future. For such dispensable items, a compromise of the treatment is: if the home space enough, first leave it at home, if the home space is not enough, even if the home of all the rubbish to remove, or can not accommodate those essential necessities of life, then throw away these dispensable items.
Starting with the JDK1.2 version, the object's references are divided into four levels, giving the program more flexibility in controlling the object's life cycle. These four levels are high to low: strong references, soft references, weak references, and virtual references

1. Strong references

Most of the references we used before are actually strong references, which are the most common references. If an object has a strong reference, it is similar to essential necessities, and the garbage collector will never recycle it. When there is not enough memory space, the Java virtual Machine prefers to throw a outofmemoryerror error, which causes the program to terminate abnormally, and does not rely on random recycling of strongly referenced objects to resolve out-of-memory issues.
2. Soft references (SoftReference)

If an object has only soft references, it is similar to a living thing that can be used. If the memory space is sufficient, the garbage collector does not reclaim it, and if the memory space is insufficient, the memory of those objects is reclaimed. The object can be used by the program as long as it is not reclaimed by the garbage collector. Soft references can be used to implement memory-sensitive caches.
A soft reference can be used in conjunction with a reference queue (Referencequeue), and if the object referenced by the soft reference is garbage collected, the Java Virtual machine will add the soft reference to the reference queue associated with it.

3. Weak references (WeakReference)
If an object has only weak references, it is similar to a living thing that can be used. The difference between a weak reference and a soft reference is that an object with only a weak reference has a shorter life cycle. As the garbage collector thread scans the area of memory it governs, once an object with only a weak reference is found, its memory is reclaimed, regardless of whether the current memory space is sufficient or not. However, because the garbage collector is a low-priority thread, it is not necessarily quick to discover objects that have only weak references.
A weak reference can be used in conjunction with a reference queue (Referencequeue), and if the object referenced by the weak reference is garbage collected, the Java virtual machine adds the weak reference to the reference queue associated with it.


4. Virtual Reference (Phantomreference)
"Virtual Reference", as the name implies, is a dummy, unlike several other references, a virtual reference does not determine the object's life cycle. If an object holds only virtual references, it can be garbage collected at any time, just as there are no references.
Virtual references are primarily used to track the activities of objects that are garbage collected. One difference between a virtual reference and a soft reference and a weak reference is that the virtual reference must be used in conjunction with the reference queue (Referencequeue). When the garbage collector prepares to reclaim an object, if it finds that it has a virtual reference, it will add the virtual reference to the reference queue associated with it before reclaiming the object's memory. The program can determine whether a virtual reference has been added to the reference queue to understand

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

In particular, the use of weak references and virtual references is seldom used in the programming of the century, because soft references can speed up the JVM's recovery of garbage memory, maintain the security of the system, and prevent the generation of memory overflow (OutOfMemory) problems.

Here is the code for the soft reference:

[C-sharp]View PlainCopy
    1. Import Java.lang. Ref.  SoftReference;
    2. Public class Test {
    3. public static void Main (string[] args) {
    4. System.  out.println ("start");
    5. A = new A ();
    6. softreference<a> sr = New softreference<a> (A);
    7. A = null;
    8. if (sr!=null) {
    9. A = Sr.get ();
    10. }
    11. else{
    12. A = new A ();
    13. sr = New softreference<a> (A);
    14. }
    15. System.     out.println ("End");
    16. }
    17. }
    18. Class a{
    19. int[] A;
    20. Public A () {
    21. A = new int[100000000];
    22. }
    23. }

Strong references to Java objects, soft references, weak references, and virtual 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.