Questions tagged [weak-references]

A weak reference is a one that makes no claim of ownership. A weak reference keeps a reference to the object in question while it is in memory, but does not prevent the memory management system from reclaiming the associated memory when the referenced object is otherwise no longer needed. Many languages feature or support various levels of weak references, such as Swift, Objective-C, Java, C#, Python, Perl and Lisp.

A weak reference is one that does not cause the memory management system to hold on to the referenced object. It is used in cases where one wants a reference to the object in question as long as it is still needed/used elsewhere, but to also not prevent it from being reclaimed by the memory management system in the course of its normal lifecycle.

Weak references are especially important where one might have two or more objects referencing each other and you want to avoid a “retain cycle” or ”strong reference cycle”, where circular combinations of strong references will prevent the memory from ever being reclaimed. One common use of weak references in conjunction with closures and callbacks.

Some garbage-collected languages feature or support various levels of weak references, such as , , , and .

Reference counting languages, such as and , use weak (and unowned) references to prevent the establishment of additional strong references. An object that has no further strong references will be deallocated.

Certain environments have further variations on weak references such as "soft", "unowned", "phantom", which have very specific meanings in those environments.

1131 questions
21
votes
4 answers

When should weak references be used?

I recently came across a piece of Java code with WeakReferences - I had never seen them deployed although I'd come across them when they were introduced. Is this something that should be routinely used or only when one runs into memory problems? If…
peter.murray.rust
  • 37,407
  • 44
  • 153
  • 217
21
votes
4 answers

"Weak reference": down to earth explanation needed

Can someone provide an explanation of a weak reference in Delphi? I noticed that the concept is often mentioned in some library/framework source code I scrutinize. I'm in a limbo and want to have a clear cut understanding of it.
menjaraz
  • 7,551
  • 4
  • 41
  • 81
20
votes
8 answers

ThreadLocal Resource Leak and WeakReference

My limited understanding of ThreadLocal is that it has resource leak issues. I gather this problem can be remedied through proper use of WeakReferences with ThreadLocal (although I may have misunderstood this point.) I would simply like a pattern…
Julien Chastang
  • 17,592
  • 12
  • 63
  • 89
20
votes
5 answers

How are weak references implemented?

I wonder how weak references work internally, for example in .NET or in Java. My two general ideas are: "Intrusive" - to add list of weak references to the most top class (object class). Then, when an object is destroyed, all the weak references…
Michal Czardybon
  • 2,795
  • 4
  • 28
  • 40
20
votes
6 answers

weakref list in python

I'm in need of a list of weak references that deletes items when they die. Currently the only way I have of doing this is to keep flushing the list (removing dead references manually). I'm aware there's a WeakKeyDictionary and a WeakValueDictionary,…
Dan
  • 33,953
  • 24
  • 61
  • 87
20
votes
2 answers

Can `weakref` callbacks replace `__del__`?

Is there any obstacle that prevents weakref from doing everything that __del__ does but with much stronger guarantees (e.g., finalize guarantees that the call will be made before the interpreter exits, and the order of calls is well-defined,…
max
  • 49,282
  • 56
  • 208
  • 355
20
votes
3 answers

How to delete every reference of an object in Python?

Supose you have something like: x = "something" b = x l = [b] How can you delete the object only having one reference, say x? del x won't do the trick; the object is still reachable from b, for example.
Juanjo Conti
  • 28,823
  • 42
  • 111
  • 133
20
votes
3 answers

Bug in WeakAction in case of Closure Action

In one of the projects I take part in there is a vast use of WeakAction. That's a class that allows to keep reference to an action instance without causing its target not be garbage collected. The way it works is simple, it takes an action on the…
Kobi Hari
  • 1,259
  • 1
  • 10
  • 25
20
votes
2 answers

Weakref and __slots__

Consider the following code: from weakref import ref class Klass(object): # __slots__ = ['foo'] def __init__(self): self.foo = 'bar' k = Klass() r = ref(k) it works but when I uncomment the __slots__ it breaks with TypeError:…
Ecir Hana
  • 10,864
  • 13
  • 67
  • 117
20
votes
3 answers

Understanding ConditionalWeakTable

I am trying to understand ConditionalWeakTable. What is the difference between class ClassA { static readonly ConditionalWeakTable OtherClassTable = new ConditionalWeakTable(); } and class…
cm007
  • 1,352
  • 4
  • 20
  • 40
20
votes
2 answers

WeakReference understanding

I want to create the dictionary of all the ViewModels. public static Dictionary vmCollection = new Dictionary(); Adding it like this vmCollection.Add(name, new WeakReference(viewModel)); And…
Learner
  • 1,490
  • 2
  • 22
  • 35
20
votes
4 answers

I have a circular reference. How can I create a weak reference in Objective-C?

I'm working on an iPhone application. I have an object of class Row that needs to release numerous objects of the class Block. Every Block currently has a property that retains an instance variable of class Row. @interface Block : UIImageView { …
Tozar
  • 976
  • 9
  • 20
19
votes
7 answers

Does WeakReference make a good cache?

i have a cache that uses WeakReferences to the cached objects to make them automatically removed from the cache in case of memory pressure. My problem is that the cached objects are collected very soon after they have been stored in the cache. The…
Rauhotz
  • 7,914
  • 6
  • 40
  • 44
19
votes
3 answers

Are C# weak references in fact soft?

The basic difference is that weak references are supposed to be claimed on each run of the GC (keep memory footprint low) while soft references ought to be kept in memory until the GC actually requires memory (they try to expand lifetime but may…
mafu
  • 31,798
  • 42
  • 154
  • 247
19
votes
3 answers

Testing/Verifying a WeakReference

I'd like to verify that code setting up a WeakReference does not accidentally hold a strong reference to the referenced object. (Here's an example of how it is easy to accidentally do this.) Does this look like the best way to check for inadvertent…
Ben Gribaudo
  • 5,057
  • 1
  • 40
  • 75