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
9
votes
4 answers

Testing WeakReference

What is the proper approach to testing a weak reference in Java? My initial idea is to do the following: public class WeakReferenceTest { public class Target{ private String value; public Target(String value){ …
John Ericksen
  • 10,995
  • 4
  • 45
  • 75
9
votes
1 answer

Any weak interning collections (for immutable objects)

In some situations involving immutable objects, it will be possible for many distinct objects to come into existence which are semantically identical. A simple example would be reading many lines of text from a file into strings. From the…
supercat
  • 77,689
  • 9
  • 166
  • 211
9
votes
1 answer

finding weak reference objects in collections in java

A couple of questions regarding Java's WeakReference and Collections: Is there a library out there that implements Java's various data-set interfaces (eg Collection, List, Set, Queue etc) with WeakReference transparently? Like WeakHashMap is for…
Petriborg
  • 2,940
  • 3
  • 28
  • 49
8
votes
1 answer

Why there is no WeakList and WeakSet implementation in Java?

... at least not "official". You can easily google two or three "WeakList" example implementation and for "WeakSet" one very good can be found in NetBeans Platform API sources. I read similar question placed here asking for WeakSet. Answers were…
Matt Warrick
  • 1,385
  • 1
  • 13
  • 22
8
votes
1 answer

How to keep track of instances of python objects in a reliable way?

I would like to be able to keep track of instances of geometric Point objects in order to know what names are already "taken" when automatically naming a new one. For instance, if Points named "A", "B" and "C" have been created, then the next…
zezollo
  • 4,606
  • 5
  • 28
  • 59
8
votes
2 answers

MATLAB weak references to handle class objects

While thinking about the possibility of a handle class based ORM in MATLAB, the issue of caching instances came up. I could not immediately think of a way to make weak references or a weak map, though I'm guessing that something could be contrived…
kenm
  • 23,127
  • 2
  • 43
  • 62
8
votes
4 answers

Swift. Is the (absolutely) sole specific advantage of unowned over weak, performance?

In Swift, we have normal default typing the object simply cannot become nil. we have weak typing the object can become nil. if the object becomes nil, your pointer automatically becomes nil, so you know that the object became nil and we have…
Fattie
  • 27,874
  • 70
  • 431
  • 719
8
votes
1 answer

weakSelf (the good), strongSelf (the bad) and blocks (the ugly)

I have read that when a block like this is executed: __weak typeof(self) weakSelf = self; [self doSomethingInBackgroundWithBlock:^{ [weakSelf doSomethingInBlock]; // weakSelf could possibly be nil before reaching this point …
agy
  • 2,804
  • 2
  • 15
  • 22
8
votes
3 answers

Weak method argument semantics

Is there any way to specify that a particular method argument has weak semantics? To elaborate, this is an Objective-C sample code that works as expected: - (void)runTest { __block NSObject *object = [NSObject new]; …
Sash Zats
  • 5,376
  • 2
  • 28
  • 42
8
votes
3 answers

weak cannot be applied to non-class type uiimageview

I have a class in swift that needs to have a weak pointer to an array of objects that is allocated in another class. I have class myView: UIView { var lines:[CAShapeLayer] = [] weak var avatars : [UIImageView]? The error I get is 'weak'…
user1709076
  • 2,538
  • 9
  • 38
  • 59
8
votes
3 answers

When do short weak references become null?

I track an object using WeakReference (short weak reference) in my class Foo. This class has a destructor in which I need to access that tracked object. The object I track is also tracking Foo using WeakReference. So now I wonder, when…
Paya
  • 5,124
  • 4
  • 45
  • 71
8
votes
2 answers

Create a weak reference to an object

Is it possible in Actionscript 3 to create a weak reference to an object, so that it can be garbage collected. I'm creating some classes to make debugging easier, so I don't want the objects to hang around in memory if they are only referenced here…
8
votes
3 answers

Get list of active items from ConditionalWeakTable

The .NET 4.0 ConditionalWeakTable is effectively a dictionary where the dictionary's keys are weak referenced and can be collected, which is exactly what I need. The problem is that I need to be able to get all live keys from this dictionary, but…
Steven
  • 166,672
  • 24
  • 332
  • 435
8
votes
2 answers

What happens to a WeakReference after GC of WeakReference.Target

What happens to the WeakReference when the target object referenced by WeakReference.Target has been garbage collected? Does the WeakRerence stay alive and keeps existing? The reason why I am asking is that I have a list of WeakReferences stored in…
bitbonk
  • 48,890
  • 37
  • 186
  • 278
8
votes
4 answers

How to get the target of a weak reference in a safe way

Consider this code: var weakRef = new WeakReference(new StringBuilder("Mehran")); if (weakRef.IsAlive) { // Garbage Collection might happen. Console.WriteLine((weakRef.Target as StringBuilder).ToString()); } It's possible for GC.Collect to…
mehrandvd
  • 8,806
  • 12
  • 64
  • 111