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
76
votes
14 answers

Pros and Cons of Listeners as WeakReferences

What are the pros and cons of keeping listeners as WeakReferences? The big 'Pro' of course is that: Adding a listener as a WeakReference means the listener doesn't need to bother 'removing' itself. For those worried about the listener having the…
pdeva
  • 43,605
  • 46
  • 133
  • 171
75
votes
3 answers

Weak reference benefits

Can someone explain the main benefits of different types of references in C#? Weak references Soft references Phantom references Strong references. We have an application that is consuming a lot of memory and we are trying to determine if this is…
leora
  • 188,729
  • 360
  • 878
  • 1,366
75
votes
5 answers

Java's WeakHashMap and caching: Why is it referencing the keys, not the values?

Java's WeakHashMap is often cited as being useful for caching. It seems odd though that its weak references are defined in terms of the map's keys, not its values. I mean, it's the values I want to cache, and which I want to get garbage collected…
mxk
  • 43,056
  • 28
  • 105
  • 132
69
votes
7 answers

Is there a SoftHashMap in Java?

I know there is a WeakHashMap in java.util, but since it uses WeakReferences for everything, which is only referenced by this Map, referenced objects will get lost on the next GC cycle. So it's nearly useless if you want to cache random data, which…
tigger
  • 1,856
  • 3
  • 18
  • 23
61
votes
2 answers

Using weak self in dispatch_async function

I read a lot of posts about using __weak self inside dispatch_async, and now I am a litle bit confused. if I have : self.myQueue = dispatch_queue_create("com.biview.core_data", NULL); dispatch_async(self.myQueue, ^(void){ if (!self.var1) { …
Marko Zadravec
  • 8,298
  • 10
  • 55
  • 97
60
votes
3 answers

WeakReference/AsyncTask pattern in android

I have a question regarding this simple frequently occurring situation in android . We have a main activity , we invoke an AsyncTask alongwith the reference of the mainactivity , so that that the AsyncTask can update the views on the…
Muhammad Ahmed AbuTalib
  • 4,080
  • 4
  • 36
  • 59
49
votes
6 answers

Strong and weak references in Swift

In Objective C you can define a property as having a strong or weak reference like so: @property(strong)... @property(weak)... How is this done in swift?
67cherries
  • 6,931
  • 7
  • 35
  • 51
48
votes
3 answers

Cost of using weak references in Java

Has anyone researched the runtime costs involved in creating and garbage collecting Java WeakReference objects? Are there any performance issues (e.g. contention) for multi-threaded applications? EDIT: Obviously the actual answer(s) will be JVM…
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
45
votes
5 answers

Is Josh Smith's implementation of the RelayCommand flawed?

Consider the reference Josh Smith' article WPF Apps With The Model-View-ViewModel Design Pattern, specifically the example implementation of a RelayCommand (In Figure 3). (No need to read through the entire article for this question.) In general,…
Greg D
  • 43,259
  • 14
  • 84
  • 117
42
votes
1 answer

How can you capture multiple arguments weakly in a Swift closure?

Is there a way to capture multiple arguments weakly in a swift closure? I know this is the syntax to capture one argument weakly: { [weak arg] arg.doSomething() } How can I do this for two objects that I wish to capture weakly?
phoganuci
  • 4,984
  • 9
  • 39
  • 50
42
votes
8 answers

Collections of zeroing weak references under ARC

How can I get an array of zeroing weak references under ARC? I don't want the array to retain the objects. And I'd like the array elements either to remove themselves when they're deallocated, or set those entries to nil. Similarly, how can I do…
40
votes
5 answers

Weak references - how useful are they?

So I've been mulling over some automatic memory management ideas lately - specifically, I've been looking at implementing a memory manager based on reference counting. Of course, everyone knows that circular references kill naive reference counting.…
Ken Wayne VanderLinde
  • 18,915
  • 3
  • 47
  • 72
40
votes
3 answers

When to use weak references in Python?

Can anyone explain usage of weak references? The documentation doesn't explain it precisely, it just says that the GC can destroy the object linked to via a weak reference at any time. Then what's the point of having an object that can disappear at…
bodacydo
  • 75,521
  • 93
  • 229
  • 319
36
votes
3 answers

Android Asyntask: Use weak reference for context to avoid device rotate screen

In Apress Pro Android 4 the author has said that: [...] context of currently running activity will no longer be valid when the device is rotated. [...] One approach is to use a weak reference to the activity instead of a hard reference [...] But…
hqt
  • 29,632
  • 51
  • 171
  • 250
36
votes
5 answers

IBOutlet and viewDidUnload under ARC

There is a similar question to this on SO here, however I just want to clarify something that wasn't fully explained there. I understand that all delegates and outlets - in fact any reference to a "parent" object, to be a good citizen and think…
Stuart
  • 36,683
  • 19
  • 101
  • 139
1
2
3
75 76