Is there a practical use for weak links?

Possible duplicate:
Weak links - how useful are they?

Since weak links can be declared by the garbage collector at any time, is there a practical reason for using them?

+82
java garbage-collection weak-references
Jan 09 '12 at 15:09
source share
9 answers

If you want to keep a link to something, if it is used elsewhere, for example. listener, you can use a weak link.

WeakHashMap can be used as a short key cache for received data. It can also be used to store information about objects used elsewhere, and you do not know when these objects will be discarded.

BTW Soft Links are similar to weak links, but they will not always be cleared immediately. The GC will always drop weak links whenever possible, and keep Soft References whenever possible.

There is another kind of link called a Phantom link. This is used in the GC cleanup process and refers to an object that is not available for "normal" code because it is in the cleanup process.

+39
Jan 09 2018-12-12T00:
source share

Since a weak link can be declared by the garbage collector at any time, is there any practical reason to use it?

Of course, there are practical reasons for using it. It would be oddly strange if the framework designers faced the enormous cost of creating a weak frame of reference that would be impractical, don't you think?

I think the question you intended to ask was:

What are realistic situations where people use weak links?

There's a lot. Common is the achievement of a performance goal. Tuning application performance often requires a trade-off between high memory usage and high time usage. Suppose, for example, that there is a complex calculation that you must perform many times, but the calculation is "clean" - the answer depends only on the arguments, and not on the exogenous state. You can create a cache map from the arguments to the result, but then it uses memory. You may never ask the question again, and this memory will be wasted.

Weak links may solve this problem; the cache can become quite large, and therefore time is saved if the same question is asked many times. But if the cache gets large enough for the garbage collector to free up space, it can do it safely.

The disadvantage, of course, is that the garbage collector's cleaning policy is configured to achieve the goals of the entire system, rather than a specific cache problem. If the GC policy and the required cache policy are fairly aligned, then weak links are a very pragmatic solution to this problem.

+25
Jan 09 '12 at 17:06
source share

If WeakReference is the only reference to the object, and you want the object to hang, you should probably use SoftReference .

WeakReferences is best used when there will be other links to the object, but you cannot (or do not want to find them) when these other links are no longer used. Then another link will prevent garbage collection of the object, and WeakReference will be just another way to get to the same object.

Two common use cases:

  • To store additional (often expensively calculated, but reproducible) information about certain objects that cannot be changed directly, and whose life cycle you have little control. WeakHashMap is an ideal way to keep these links: the key in WeakHashMap is only weakly held, and therefore, when the key is garbage collection, the value can also be deleted from the card, and therefore garbage will be collected.
  • To implement any system of events or notifications where the “listeners” are registered with some kind of coordinator, so they can be informed when something happens, but where you do not want these listeners to not be garbage collected when they end in their life. A WeakReference will point to an object while it is still alive, but points to "null" as soon as the original object was garbage collected.
+12
Jan 09 '12 at 19:25
source share

We use it for this reason - in our example, we have many listeners who must register with the service. The service maintains weak references to listeners, while instance classes keep strong references. If classes receive GC'ed at any time, then a weak reference is all that remains of the listeners, who will then be GC'ed. This makes it easy to track intermediate classes.

+11
Jan 09 2018-12-12T00:
source share

The most common use of weak links for values ​​in the "search" Maps.

With normal (hard) value references, if the value on the map no longer refers to it elsewhere, you often don't need a search. If the map value is weakly referenced, as soon as there are no other references to it, the object becomes a candidate for garbage collection

The fact that the map itself has a (single) link to the object does not prevent it from collecting garbage, because the link is a weak link

+8
Jan 09 '12 at 15:25
source share

To prevent memory leak, see the article for details.

+3
Jan 09 2018-12-12T00:
source share

A weak link is a link that does not protect the referent object from collection by the garbage collector.

  • We consider an object that is referenced only by weak links inaccessible (or "inaccessible"), and therefore they can be time consuming.
  • Weak references are used to avoid saving memory referenced by unnecessary objects. Some garbage related functions support or support various levels of weak references, such as Java, C #, Python, Perl, or Lisp.
  • Garbage collection is used to reduce the potential for memory leaks and data corruption. There are two main types of garbage collection: tracking and link counting. Link counting schemes record the number of links to a given object and the collection of the object when the link count becomes zero. Link counting cannot be circular (or circular) links, since only one object can be collected at a time. Groups of objects of mutual reference, which do not have a direct link to other objects and are inaccessible, can thus reside permanently; if the application constantly generates such unreachable groups of unreachable objects, this will have a memory leak effect. Weak links can be used to solve the problem of circular links if control loops are avoided by using weak links for some links within the group.
  • Weak links are also used to minimize the number of unnecessary objects in memory, allowing the program to indicate which objects are not critical, only weakly referring to them.
+2
Jan 09 '12 at 15:33
source share

I usually use it for a certain type of cache. Recently available items are available immediately, and in case of a miss in the cache you reload the item (DB, FS, whatever).

+2
09 2018-12-12T00:
source share

I use WeakSet to encode links on a chart. If the node element is removed, the links will automatically disappear.

0
Jan 09 '12 at 18:34
source share



All Articles