Java weakreference and softreference

Source: Internet
Author: User

Java. Lang. Ref is introduced in Java 1.2, and weakreference belongs to this package. What does weakreference do? The disadvantage is that it is related to garbage collection in Java. If only weakreference references an object, the object may be recycled by the garbage collector.

Where can we use weakreference?

(1) Sometimes we encounter some classes that cannot be inherited, such as final class, which cannot be inherited. If we want to use a widget class, we cannot inherit this class to add a function for some reason. However, we must associate each widget object with a serial number, and the widget itself does not have the serial number attribute. What should we do?

You may have thought that using hashmap: serialnumbermap. Put (widget, widgetserialnumber); this looks good. But there is a problem: when we no longer need the serial number information of a widget, we should remove this entry from hashmap. What should we do if we forget it? Because hashmap holds a reference to this object, this object will never be recycled by the garbage collector, which causes memory leakage! This means that we need to manually manage the memory like a language without the garbage collection function! But we use Java.

(2) another common problem is caching. If strong references are used, the cached objects will remain in the memory and will not be recycled unless we manually remove them from the cache. In addition, this requires us to decide when to remove objects from the cache and another issue of manual memory management! In this case, weakreference shows its value. How to Create a weakreference?

WeakReference<widget> weakWidget = newWeakReference<widget>(widget);Widget w = weakWidget.get();

It should be noted that when weakreference. Get () is called, null may be returned (meaning that the object to which it points has been recycled ). In fact, the simplest way to solve the problem of the widget serial number is to use weakhashmap, which is exactly the same as the normal hashmap. The difference is that, the weakhashmap key is implemented as a weakreference (note that it is a key instead of a value). When the key object is recycled, weakhashmap automatically removes the corresponding entry. More precisely, the existence of the ing of a given key does not prevent the garbage collector from recycling the key.
(3) there are four types of references in Java: strong reference> soft reference> weakreference> phantom reference. Among them, we usually use strong reference, but Phantom reference is rarely used. So what is soft reference?

The difference between soft reference and weak reference is that once the GC finds that the object is weak reference reachable, it will be put in referencequeue, and then it will be reclaimed at the next GC; when the object is soft reference, GC may apply for more memory from the operating system, rather than directly reclaim it. Such as the cache system, which is most suitable for soft reference. Let GC decide when to recycle objects and what objects to recycle for us. The difference is as follows: weakreference Model

A obj = new ();
Weakreference wR = new weakreference (OBJ );
OBJ = NULL;
// Wait for a while and the OBJ object will be reclaimed
...
If (WR. Get () = NULL)
{
System. Out. println ("OBJ has been cleared ");
} Else {
System. Out. println ("OBJ has not been cleared, and its information is" + obj. tostring ());
}

 

Http://blog.csdn.net/ponderforever/article/details/7299069 ()

Http://wenku.baidu.com/view/8c04b0d249649b6648d74786.html ()

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.