Objective
This paper attempts to explore the weak references in Java from three angles of what, Why and how to help us understand the definition, basic usage scenarios and usage of weak references in Java.
First, what--what is a weak reference?
A weak reference in Java refers specifically to a java.lang.ref.WeakReference<T>
class, and we first look at the official document's description of it:
The presence of a weakly referenced object does not prevent the object it points to from being reclaimed by the garbage collector. The most common use of weak references is to implement canonical mappings (canonicalizing mappings, such as hash tables).
Assuming that the garbage collector determines at a point in time that an object is weakly accessible (weakly reachable) (that is, all the weak references currently pointing to it), the garbage collector clears all weak references to the object and then marks the weakly accessible object as verifiable (finalizable ), so that it will then be recycled. At the same time or later, the garbage collector puts the weak references just cleared into the reference queue (Reference queue) that was specified when the weak reference object was created.
In fact, there are four references in Java, which are strong to weak, in turn: strong references , soft references , weak references , and virtual references .
Here's a brief description of three other references except the weak reference:
1, strong reference (strong Reference): Usually the reference we return when we create a new object by using new is a strong reference, and if an object is reachable through a series of strong references, it is strong (strongly reachable) and it is not recycled.
2, soft Reference (Soft Reference): The difference between a soft reference and a weak reference is that if an object is a weak reference, it will be recycled regardless of whether the current memory is sufficient, and the soft reference can be recycled when the memory is not sufficient, so soft references are stronger than weak references.
3. Virtual reference (Phantom Reference): Virtual reference is the weakest reference in Java, so how weak is it? It is so fragile that we cannot even get the referenced object through a virtual reference, the only effect of a virtual reference is when the object it points to is reclaimed, the virtual reference itself is added to the reference queue, and the object it points to is reclaimed.
Second, why why--use weak reference?
Consider the following scenario: Now there is a Product
class that represents a product that is designed to be extensible, and at this point we want to add a number to each product. One solution is to use the HashMap<Product, Integer>
. So the question is, if we no longer need an Product
object in memory (for example, if we've sold this product), suppose the reference to it is, productA
we're going to productA
assign it null
, and then the objects that we used to point to are productA
Product
not recycled, Because it's clearly being HashMap
quoted. So in this case, we want to really recycle an Product
object, it's not enough to just assign a strong reference to it null
, but to remove the corresponding entry from HashMap
the. Obviously " HashMap
remove unwanted entries from" This work we do not want to do ourselves, we want to tell the garbage collector: in the case of only the reference to the HashMap
key
Product
object, you can recycle the corresponding Product
object. Obviously, using weak references can help us achieve this by defining the previous weak reference. All we need to do is use a Product
weak reference object that points to the object HashMap
key
.
Iii. How do how--use weak references?
For example, we use a weak reference object that points to an Product
object HashMap
key
, and we simply define this weak reference object:
Product ProductA = new product (...);
weakreference<product> weakproducta = new weakreference<> (PRODUCTA);
Now, if the reference object weakProductA
is pointing to the Product
object productA
. So how do we weakProduct
get the object that it's pointing to Product
productA
?
It's simple, just the following code:
Product Product = Weakproducta.get ();
In fact, in this case, the Java class Library provides us with WeakHashMap
classes, using and this class, whose keys are naturally weakly referenced objects, without the need for us to manually wrap the original objects. Thus, when productA
it becomes a null
time (indicating that it Product
does not need to exist in memory), then the object Product
is pointed to by a weak reference object weakProductA
, and then obviously the corresponding Product
object is weakly accessible at this time, So the weak reference to it is cleared and the Product
object is reclaimed, and the weakly referenced object to it goes into the reference queue.
Iv. reference queues
Let's briefly introduce the concept of the following reference queue. In fact, the WeakReference
class has two constructors:
Creates a weak reference WeakReference (T referent) that points to the given object
//Creates a weak reference
WeakReference (T referent that points to the given object and enlists to the given reference queue, referencequeue<? Super T> Q)
We can see that the second constructor provides a ReferenceQueue
type of argument that, by providing this argument, registers the created weak reference object on a reference queue so that when it is purged by the garbage collector, it is sent into the reference queue. We can then manage the weakly referenced objects that are cleared.
V. Summary
Well, the content of this article to this end, because the personal level is limited, the narrative inevitably exists inaccurate or unclear place, I hope you can point out, thank you for your support for the cloud habitat community.