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
907
votes
12 answers

What's the difference between SoftReference and WeakReference in Java?

What's the difference between java.lang.ref.WeakReference and java.lang.ref.SoftReference ?
driekken
  • 11,443
  • 4
  • 19
  • 10
268
votes
6 answers

Always pass weak reference of self into block in ARC?

I am a little confused about block usage in Objective-C. I currently use ARC and I have quite a lot of blocks in my app, currently always referring to self instead of its weak reference. May that be the cause of these blocks retaining self and…
the_critic
  • 12,720
  • 19
  • 67
  • 115
264
votes
7 answers

What is the difference between a weak reference and an unowned reference?

Swift has: Strong References Weak References Unowned References How is an unowned reference different from a weak reference? When is it safe to use an unowned reference? Are unowned references a security risk like dangling pointers in C/C++?
229
votes
18 answers

How do I declare an array of weak references in Swift?

I'd like to store an array of weak references in Swift. The array itself should not be a weak reference - its elements should be. I think Cocoa NSPointerArray offers a non-typesafe version of this.
Bill
  • 44,502
  • 24
  • 122
  • 213
212
votes
7 answers

Java: difference between strong/soft/weak/phantom reference

I have read this article about different types of references in Java (strong, soft, weak, phantom), but I don't really understand it. What is the difference between these reference types, and when would each type be used?
user1204873
189
votes
4 answers

How to use WeakReference in Java and Android development?

I have been a Java developer for 2 years. But I have never written a WeakReference in my code. How to use WeakReference to make my application more efficient, especially the Android application?
Chris
  • 6,431
  • 10
  • 44
  • 59
168
votes
10 answers

When would you use a WeakHashMap or a WeakReference?

The use of weak references is something that I've never seen an implementation of so I'm trying to figure out what the use case for them is and how the implementation would work. When have you needed to use a WeakHashMap or WeakReference and how was…
18Rabbit
  • 3,191
  • 2
  • 25
  • 24
103
votes
1 answer

Where does the weak self go?

I often do this, let when = DispatchTime.now() + 2.0 DispatchQueue.main.asyncAfter(deadline: when) { beep() } and in one app we often do this tickle.fresh(){ msg in paint() } but if you do this let when = DispatchTime.now() +…
Fattie
  • 27,874
  • 70
  • 431
  • 719
103
votes
9 answers

Is it possible to create a "weak reference" in JavaScript?

Is there any way in JavaScript to create a "weak reference" to another object? Here is the wiki page describing what a weak reference is. Here is another article that describes them in Java. Can anyone think of a way to implement this behavior in…
Stephen Cagle
  • 14,124
  • 16
  • 55
  • 86
102
votes
8 answers

Is there a practical use for weak references?

Possible Duplicate: Weak references - how useful are they? Since weak references can be claimed by the garbage collector at any time, is there any practical reason for using them?
user496949
  • 83,087
  • 147
  • 309
  • 426
87
votes
11 answers

Why can the keyword "weak" only be applied to class and class-bound protocol types

When I'm declaring variables as weak in Swift, I sometimes get the error message from Xcode: 'weak' may only be applied to class and class-bound protocol types or 'weak' must not be applied to non-class-bound 'SomeProtocol'; consider adding a…
Thor
  • 9,638
  • 15
  • 62
  • 137
82
votes
2 answers

Why does exist WeakHashMap, but absent WeakSet?

From J. Bloch A ... source of memory leaks is listeners ... The best way to ensure that callbacks are garbage collected promptly is to store only weak references to them, for instance, by storing them only as keys in a …
Stan Kurilin
  • 15,614
  • 21
  • 81
  • 132
82
votes
4 answers

Understanding Java's Reference classes: SoftReference, WeakReference, and PhantomReference

Can someone explain the difference between the three Reference classes (or post a link to a nice explanation)? SoftReference > WeakReference > PhantomReference, but when would I use each one? Why is there a WeakHashMap but no SoftHashMap or…
81
votes
4 answers

What is the difference between a __weak and a __block reference?

I'm reading Xcode's documentation, and here is something that puzzles me: __block typeof(self) tmpSelf = self; [self methodThatTakesABlock:^ { [tmpSelf doSomething]; }]; The following is copied from the documentation: A block forms a strong…
77
votes
2 answers

How does weak_ptr work?

I understand how to use weak_ptr and shared_ptr. I understand how shared_ptr works, by counting the number of references in its object. How does weak_ptr work? I tried reading through the boost source code, and I'm not familiar enough with boost to…
Oliver Zheng
  • 7,831
  • 8
  • 53
  • 59
1
2 3
75 76