It is interesting to find the weakref module in the standard class library of Python. I vaguely remember that in Java, there seems to be a class called weakreference, but I didn't pay too much attention to it. At this time, I started to be curious. I found this article on the Internet and spoke quite well.
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?
Sometimes we encounter some class that cannot be inherited, such as final class, or an object created through factory, which exposes only one interface externally, and we cannot know its specific implementation class, naturally, it cannot be inherited. If we want to use a widget class, for some reason, we cannot inherit this class to add a function. 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 of using hashmap! Yes, like this:
1. serialnumbermap. Put (widget, widgetserialnumber );
This looks good. However, 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 .....
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? Simple:
1. weakreference <widget> weakwidget = newweakreference <widget> (widget); 2. 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.
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 we will not study it here. 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.