WeakReference
and WeakReference<T>
in the System
namespace. These classes are distinguished by the fact that the second one is a generic type, that is, it allows you to specify the type of the object to which the link points, in addition, they have different interfaces for accessing the object that the link points to: the first one uses this properties IsAlive
and Target
, and the second - methods TryGetTarget
and SetTarget
.bool
: if its value is false
, then a normal weak link is created, and if true
, the link will behave like a PhantomReference
in Java. In C #, weak links have no finalizers, as well as other ways to quickly find that one of the many weak links has been reset.std::weak_ptr
class is responsible for weak references, which is intended to be used in conjunction with the std::shared_ptr
class. To work with an object to which a weak link points, you first need to get a strong link using the lock
method. #include <iostream> #include <memory> using namespace std; void print_weak(weak_ptr<int> w) { if (auto s = w.lock()) { cout << *s << endl; } else { cout << " " << endl; } } int main(int argc, char *argv[]) { shared_ptr<int> s(new int(123)); weak_ptr<int> w(s); print_weak(w); s = NULL; print_weak(w); }
Weak
, which is defined in the System.Mem.Weak
module. In Haskell, a weak link will exist at least as long as the key is reachable by reference, so you can create a weak link with the finalizer, “forget” about it, and the finalizer will still be called. The addFinalizer
function addFinalizer
just that. To create a weak link, there are functions mkWeak
and mkWeakPtr
. import Control.Exception import System.Mem.Weak main = do l <- getLine mkWeakPtr l (Just $ putStrLn " ") evaluate $ sum [1..100000]
get
method. There are as many as 3 kinds of weak links: “soft” links (class SoftReference
in the java.lang.ref
), common weak links ( WeakReference
) and “phantom” links ( PhantomReference
). They differ in that when the garbage collector will release objects reachable by such links. If an object is reachable via soft links, it will be freed only if there is not enough free space on the heap. Such links are well suited for all kinds of caches. Normal weak links are different from “phantom” ones when the link is reset: the first ones are reset before the finalizer is called, and the second ones after, just before the memory is released. To prevent access to finalized objects, the read function for the PhantomReference
always returns null
. The only way to benefit from them is to use link queues.ReferenceQueue
— and set up weak links so that they add themselves when they are zeroed out. Now you can check not all links, but only one queue.WeakHashMap
- an analogue of HashMap
with weak references to keys. import java.lang.ref.*; class WeakRefTest implements Runnable { // , // , . public static void printWhenUnreachable(Object obj) { ReferenceQueue<Object> queue = new ReferenceQueue<Object>(); WeakReference<Object> ref = new WeakReference<Object>(obj, queue); new Thread(new WeakRefTest(ref, queue)).start(); } private final WeakReference<Object> ref; private final ReferenceQueue<Object> queue; private WeakRefTest(WeakReference<Object> ref, ReferenceQueue<Object> queue) { this.ref = ref; this.queue = queue; } public void run() { try { queue.remove(); System.out.println(" "); } catch (InterruptedException e) { throw new RuntimeException(e); } } }
WeakRefTest
class has a ref
field that is not used. It is necessary so that the weak link itself is not released prematurely, otherwise it will not work.__mode
key can be set to a string value, and then if it contains the k
character, then the keys references will be weak, and if it contains the v
character, then the references to values will be weak . The type of the table can be changed after its creation, but the changes will take effect only at the next garbage collection. As in other implementations of tables with weak references, when a key or value is zeroed out, the entire record is deleted.weaken
method of the Scalar::Util
package. Unlike other languages, if you copy a weak link, the copy will be a regular link.ref
(package weakref
), the usual link to the object must be obtained explicitly, and with instances of the class proxy
you can work directly.WeakSet
, WeakKeyDictionary
and WeakValueDictionary
. # -*- coding: utf8 -*- from weakref import * def finalizer(ref): print(" ") obj = set() reference = ref(obj, finalizer) del obj
WeakRef
class, which behaves similarly to the proxy
class in Python, that is, you can work with a weak link in the same way as with the object itself. In addition to the methods of the object itself, weak links have a weakref_alive?
method weakref_alive?
, which returns true
if the object to which the link points still exists, otherwise false
.Source: https://habr.com/ru/post/163679/
All Articles