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

Good implementation of weak dictionary in .Net

Where can I find good implementation of IDictionary which uses weak references inside? Dictionary should be holding only weak references to values and eventually clean up itself of dead references. Or should I just write it myself?
Konstantin Spirin
  • 20,609
  • 15
  • 72
  • 90
34
votes
1 answer

JVM G1GC's mixed gc not collecting much old regions

My server is using 1.8.0_92 on CentOS 6.7, GC param is '-Xms16g -Xmx16g -XX:+UseG1GC'. So the default InitiatingHeapOccupancyPercent is 45, G1HeapWastePercent is 5 and G1MixedGCLiveThresholdPercent is 85. My server's mixed GC starts from 7.2GB, but…
koji lin
  • 667
  • 1
  • 8
  • 12
34
votes
4 answers

WeakReference and event handling

Is it a good practice to implement event handling through WeakReference if that event is the only thing holding the reference and that we would need the object to be garbage collected? As an argument to this: Folks say that if you subscribe to…
Cherian
  • 19,107
  • 12
  • 55
  • 69
34
votes
10 answers

Generic typeof for weak self references

I am trying to figure out a way to use typeof to create a weak reference to self for use in blocks to avoid retain cycles. When I first read about this it seems that the convention was to use __block typeof(self) bself = self;, which compiles but…
33
votes
1 answer

Should ConditionalWeakTable be used for non-compiler purposes?

I've recently come across the ConditionalWeakTable class in my search for an IDictionary which uses weak references, as suggested in answers here and here. There is a definitive MSDN article which introduced the class and which…
rikoe
  • 1,639
  • 1
  • 21
  • 29
32
votes
2 answers

Is self captured within a nested function

With closures I usually append [weak self] onto my capture list and then do a null check on self: func myInstanceMethod() { let myClosure = { [weak self] (result : Bool) in if let this = self { …
31
votes
2 answers

weak or strong for IBOutlet and other

I have switched my project to ARC, and I don't understand if I have to use strong or weak for IBOutlets. Xcode do this: in interface builder, if a create a UILabel for example and I connect it with assistant editor to my ViewController, it create…
Piero
  • 9,173
  • 18
  • 90
  • 160
30
votes
5 answers

What's the difference between ES6 Set and WeakSet?

ECMAScript 6 has these very similar collections: Set and WeakSet. What is the difference between them?
Luboš Turek
  • 6,273
  • 9
  • 40
  • 50
29
votes
1 answer

Set equivalent of WeakHashMap?

Is HashSet> the Set equivalent of WeakHashMap? That is, will entries be automatically deleted when they are no longer referenced? If not, what is the equivalent?
Barry Fruitman
  • 12,316
  • 13
  • 72
  • 135
28
votes
4 answers

Why isn’t my weak reference cleared right after the strong ones are gone?

I am a little bit stubborn, but I want to understand weak and strong references well, so that's why I'm asking you once again. Consider this: __weak NSString* mySecondPointer = myText; NSLog(@"myText: %@", myText); The result is myText: (null)…
28
votes
4 answers

Using Java's ReferenceQueue

Do SoftReference and WeakReference really only help when created as instance variables? Is there any benefit to using them in method scope? The other big part is ReferenceQueue. Besides being able to track which references are determined garbage,…
bgroenks
  • 1,859
  • 5
  • 34
  • 63
28
votes
8 answers

HashMap with weak values

I'm implementing a cache for Objects stored persistently. The idea is: Method getObjectFromPersistence(long id); ///Takes about 3 seconds Method getObjectFromCache(long id) //Instantly And have a method: getObject(long id) with the following…
Addev
  • 31,819
  • 51
  • 183
  • 302
28
votes
9 answers

Weak events in .NET?

If object A listens to an event from object B, object B will keep object A alive. Is there a standard implementation of weak events that would prevent this? I know WPF has some mechanism but I am looking for something not tied to WPF. I am guessing…
tom7
  • 4,062
  • 8
  • 30
  • 30
27
votes
1 answer

Is it possible to create a truely weak-keyed dictionary in C#?

I'm trying to nut out the details for a true WeakKeyedDictionary<,> for C#... but I'm running into difficulties. I realise this is a non-trivial task, but the seeming inability to declare a WeakKeyedKeyValuePair<,> (where the GC only follows the…
Mania
  • 1,718
  • 14
  • 16
27
votes
3 answers

How do events cause memory leaks in C# and how do Weak References help mitigate that?

There are two ways (that I know of) to cause an unintentional memory leak in C#: Not disposing of resources that implement IDisposable Referencing and de-referencing events incorrectly. I don't really understand the second point. If the source…
jnvjgt
  • 273
  • 1
  • 3
  • 4
1 2
3
75 76