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

What are the benefits to using WeakReferences?

I have some memory leaks in my app. They all originate around a specific view cluster that I have spent a loooot of time tweaking and trying to reduce a much contextual passing as possible. This leads me to believe that bitmaps used in the cluster…
ahodder
  • 11,353
  • 14
  • 71
  • 114
11
votes
3 answers

Why doesn't the weakref work on this bound method?

I have a project where i'm trying to use weakrefs with callbacks, and I don't understand what I'm doing wrong. I have created simplified test that shows the exact behavior i'm confused with. Why is it that in this test test_a works as expected, but…
Asa Ayers
  • 4,854
  • 6
  • 40
  • 57
11
votes
1 answer

What did I do that made "weak refs processing" take 30 sec instead of 1.5 sec?

The story My server is running with 24x2 processors, and the java heap is about 70 GB. At some point after installing a new version (version-B), I saw that Full GC is taking around 30sec (stopping all threads). After enabling the…
Erez Makavy
  • 111
  • 3
11
votes
1 answer

Generic Array of weak references to class bound protocol in Swift 4.1

I'm trying to create a generic WeakReference type that I can put into an array (and ultimately create a generic weak array type). So far so good, but the following code: class WeakReference { weak var element:…
FSMaxB
  • 2,280
  • 3
  • 22
  • 41
11
votes
1 answer

How do you create a weak reference to an object in Python?

How do you create a weak reference to an object in Python?
readonly
  • 343,444
  • 107
  • 203
  • 205
11
votes
1 answer

Delphi "Supports" increments reference count on [weak] or [unsafe] interfaces

When I am using Delphi Berlin 10.1 [weak] (and [unsafe]) reference, the "Supports" function and "QueryInterface" both are incrementing the reference count when given an interface variable marked with the "weak" attribute (same behavior with "unsafe"…
Dave Olson
  • 1,435
  • 1
  • 9
  • 16
11
votes
3 answers

Why would I not use a weak pointer to self in a Block passed to dispatch_after()?

I have seen the following used: double delayInSeconds = 2.0; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ //code to be executed on the main…
11
votes
1 answer

Would a "circular" reference be treated as "reachability" for a WeakMap?

function f() { const w = new WeakMap(); const o = {}; w.set(o, { v: o }); return w; } const weakMap = f(); For the given code, would the only weakMap item considered as reachable or not? Hence, will it be garbage collected or not? PS:…
zerkms
  • 249,484
  • 69
  • 436
  • 539
11
votes
2 answers

Is it a good idea to implement a C# event with a weak reference under the hood?

I have been wondering whether it would be worth implementing weak events (where they are appropriate) using something like the following (rough proof of concept code): class Foo { private WeakEvent _explodedEvent = new…
Lea Hayes
  • 62,536
  • 16
  • 62
  • 111
11
votes
1 answer

Iterating a WeakHashMap

I'm using a WeakHashMap concurrently. I want to achieve fine-grained locking based on an Integer parameter; if thread A needs to modify a resource identified by Integer a and thread B does the same for resource identified by Integer b, then they…
Timmos
  • 3,215
  • 2
  • 32
  • 40
11
votes
3 answers

Can a conforming C# compiler optimize away a local (but unused) variable if it is the only strong reference to an object?

See also these related resources: Does the .NET garbage collector perform predictive analysis of code? (on Stack Overflow) WP7: When does GC Consider a Local Variable as Garbage (blog article on MSDN) In other words: Can an object referenced…
11
votes
1 answer

Combo of IdentityHashMap and WeakHashMap

I need a Map implementation that shares properties of both IdentityHashMap and WeakHashMap (reference equality instead of equals() and weak references on keys). What implementation do you recommend (it has to work on Android)?
zduny
  • 2,481
  • 1
  • 27
  • 49
11
votes
1 answer

Implementing weak intrusive pointers in C++

Weak pointers are like smartpointers, except that references from weak pointers do not prevent garbage collection, and weak pointers must have their validity checked before they are used. In our project (Linderdaum Engine…
Sergey K.
  • 24,894
  • 13
  • 106
  • 174
11
votes
6 answers

C#: Notification before WeakReference is collected?

In C#/.NET, is there any way to get a notification before the object pointed to by a weak reference is destructed? Basically, I want to allow an object to be collected, but do something right before the object is destroyed, without modifying code to…
Robert Fraser
  • 10,649
  • 8
  • 69
  • 93
11
votes
2 answers

WeakReferences are not freed in embedded OS

I've got a strange behavior here: I get a massive memory leak in production running a WPF application that runs on a DLOG-Terminal (Windows Embedded Standard SP1) that behaves perfectly fine if I run it localy on a normal desktop (Win7 prof.) After…
Random Dev
  • 51,810
  • 9
  • 92
  • 119