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

Other uses of weak references?

I know that weak references are a good candidate for memoizing potentially large sets of data, and Wikipedia's article on weak references only lists "keeping track of the current variables being referenced in the application" and the statement…
Mark Rushakoff
  • 249,864
  • 45
  • 407
  • 398
8
votes
2 answers

Weak reference with a condition / timeout

is it possible in Java to create a weak reference whose object can be sent to garbage collector only if a specified condition returns true? Let's say that I have something like a cache which maps ID numbers to some data: Map cache…
Martin Majer
  • 3,274
  • 4
  • 23
  • 36
8
votes
2 answers

Java: Stack with weak references

In Java there is a data structure called a WeakHashMap that stores weak references as keys. Whenever the weak references are taken out of memory the entry is removed from the map. If I have a data structure such as a Stack or a Set where I am…
8
votes
4 answers

When to use Weak and Phantom references in Java

I read many articles, but I don't understand - where do I need to use Weak and Phantom references in practice? Soft references - is a good choice for cache, as I understand. But weak and phantom, I don't know when to use. Please provide examples of…
7
votes
2 answers

Problems with the GC when using a WeakValueDictionary for caches

According to the official Python documentation for the weakref module the "primary use for weak references is to implement caches or mappings holding large objects,...". So, I used a WeakValueDictionary to implement a caching mechanism for a long…
Elmar Zander
  • 1,338
  • 17
  • 32
7
votes
2 answers

ArrayList> - How to tidy up best?

A quick question in between: I have a simple WeakRunnableList. Is this way ok to clean it up (removing dead references), or is there a more elegant and faster solution. Full source for my WeakRunnableList: public class WeakRunnableList { private…
Christian Ruppert
  • 3,749
  • 5
  • 47
  • 72
7
votes
2 answers

WeakReference doesn't returns null, though there are no strong references to the actual reference object

I am going through the following post on Weak References in java :- Understanding Weak References. After going through the theoretical portion, trying to test weak reference for null condition. But, null check for weak reference never returns true…
mogli
  • 1,549
  • 4
  • 29
  • 57
7
votes
1 answer

Dict or WeakKeyDictionary with identity equality -- wrap unhashable objects to check identity

I want to use some objects as keys for some dict, which are either unhashable, or hashable but I want to overwrite their __eq__/__hash__ with the default object.__eq__/object.__hash__, i.e. namely a == b iff a is b. (These objects could e.g. be…
Albert
  • 65,406
  • 61
  • 242
  • 386
7
votes
1 answer

EXC_BAD_ACCESS on objc_setAssociatedObject with -weak_library /usr/lib/libSystem.B.dylib linker flags

I have a EXC_BAD_ACCESS when I call objc_setAssociatedObject with the linker flags : -weak_library /usr/lib/libSystem.B.dylib linker flags. I absolutely need the linker flag because of this, do somebody know a workaround? (I also have a crash on…
gcamp
  • 14,622
  • 4
  • 54
  • 85
7
votes
2 answers

Can Java garbage collect variables before end of scope?

Suppose we have a program like this: void main() { // Point 0 BigThing bt = new BigThing(); // Point 1 WeakReference weak = new WeakReference<>(bt); // Point 2 doSomething(weak); // Point 3 } void…
Nayuki
  • 17,911
  • 6
  • 53
  • 80
7
votes
1 answer

Strange effect of C# closures on garbage collector

Straight to the code class Program { private static WeakReference> _noClosure; private static WeakReference> _closure; static void Main(string[] args) { Init(); …
ironic
  • 8,368
  • 7
  • 35
  • 44
7
votes
1 answer

Xamarin garbage collector and circular references

While reading Xamarin docs under section "Performance", I've noticed the following chapter: The following diagram illustrates a problem that can occur with strong references: Object A has a strong reference to object B, and object B has a strong…
Lou
  • 4,244
  • 3
  • 33
  • 72
7
votes
1 answer

Is there way to check a `unowned` (actually `unowned(safe)`) reference has been deinited?

Is there any way to check an unowned(safe) Swift reference for "availability"? So, I am looking for a hypothetical function like isReferenceAccessible in this example: func someMethod() { someAsyncOperation(parameters) { [unowned(safe) self] in …
7
votes
2 answers

didSet for weak reference not working as expected

I have this small Swift script, which uses weak references: #!/usr/bin/env swift class Thing { deinit { print("Thing object deallocated") } } class WeakThing { weak var thing: Thing? { didSet { …
user4691305
7
votes
4 answers

Creating a temporary async timer callback to a bound method with python-asyncio

I'm trying to create a sort of timer callback to a bound async method using asyncio's event loop. The problem now is that the bound async method should not hold a strong reference to the instance, otherwise the latter will never be deleted. The…
pumphaus
  • 91
  • 1
  • 6