Reference Learning in Java

Source: Internet
Author: User
Tags garbage collection

Reference learning in Java talks about what reference think

Reference provides a way to interact with the JVM GC, referring to reference, which should have some nouns in the brain, GC, Referencequeue, SoftReference, WeakReference, Phantomreference, Finalreference, and the most common strong references. I think when a small white start learning reference should think of a problem, strong citation is not good enough, why I also need weak references, soft references and so on these things.

Why do we need them?

Applications often encounter a situation in which some classes are naturally unsuitable for extension, these classes may be marked for final, or perhaps more complex, it is returned by a factory method, you do not know its specific implementation class, you just know it is an interface, but you want to expand them, So you may have used a map,key is their example, value is some of the extended attributes you want to use, such as a name, Ethan Nicholas's blog has an example:
The Widget class cannot be extended, and we want to give these objects some serial number widgetserialnumber, so we might do this:

serialNumberMap.put(widget, widgetSerialNumber);

It looks nice, but this map has a strong reference to the widget, and when the class is no longer needed, we may need to delete it manually, otherwise it can't be GC. It seems that the problem goes back to the time when there is no GC (C + + manages memory manually) and we have to always consider the ownership of the memory and release it when it should be released, which is too much java.

So some nice features come out to help you solve this problem,

new WeakReference(widget);

With a weak-reference wrapper, the JVM can then clean up the tuning at the right time when it makes the accessibility analysis. So we might accidentally find that weakwidget.get () returns null in the code, stating that it has been recycled by GC.

Reference queues

No reference object can pass an example of a Referencequeue object when it is built, which is a key to the GC when we see it as a queue from the name (look at the source code I think the stack is more appropriate, because it is always inserted in the head, Head out delete), is actually a linked list, the elements of the list is the Reference object, you can perform some basic queue, delete operations, notice that the remove is blocked, and poll does not block but immediately return NULL if the queue has no elements.

Why say it is a key, because generally in use we will construct a Referencequeue sample object, and then construct a weak reference after the type of time passed, so that when they are recycled by GC, the Remove method will return to the GC example, we can do some cleanup work , this model is very much like the event model, of course, for WeakReference, the queue is not necessarily GC, because there is a finalize method exists, may revive some objects, but for phantomreference, the queue of this memory must be GC, So phantomreference can be used as a GC time listener (with get to a small trick), but it's not very convenient to use.

The strength of the reference

Obviously, from the name you can see that the reference strength is from large to small: strong > Soft > Weak > Phantom.

Where soft and weak are actually similar, the difference is that the weak reference will be recycled once the GC is in place, and the soft reference will be recycled as long as the memory is not available.

Phantom references more than the same, it is almost impossible to hold an object, it gets () always return null, the only function is to take out from the referencequeue to use, when it comes out we know it has been recycled, We can be very relieved to do some of the recycling action.

When I was learning, I have been trying to understand that they are recycled as a GC, which is more than reassuring to use, but when I come into contact with a few demo code, I understand that they have never been in a single-soldier combat, basically their use will be with strong references.

Therefore, the object Accessibility analysis is very important, in fact, the above situation is accurately said to be weak or soft accessibility or phantom accessibility.

Here's a look at the illustrations in the reference of the Java Learning Tutorial:

Then garbage collection will be based on two principles to determine the accessibility of the object:

    1. In a single path, whichever is the weakest reference

    2. Multipath, whichever is the strongest reference.

With these two words all the answers came out.

Some of the actual examples are in the back, digging a hole first.

Reference Learning in Java

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.