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
12
votes
1 answer

How to implement a canonicalizing mapping in Java?

I am currently rolling my own little ORM, and find myself faced with the task of creating a canonicalizing mapping in order to prevent loading the same entity from the database more than once. My current approach is to use a HashMap
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
12
votes
1 answer

Is there a way to FORCE weak and/or soft referenced objects to be GC'd in Java?

Here's my use case. We are trying to narrow down a potential memory leak in an application, and we are using a memory analysis tool to snapshot the heap so we can look for object instances and references. (In case it helps, we're using…
12
votes
1 answer

Defining python type hints for list of a weakref object

I haven't found how to give type hints indication when using weakrefs. from typing import List import weakref class MyObject: def __init(self, foo) self.foo = foo o1 = MyObject(1) o2 = MyObject(2) my_list: List[weakref] =…
beesleep
  • 1,322
  • 8
  • 18
12
votes
2 answers

Weak reference instead of getActivity() (Android avoid memory leak)?

To avoid a memory leak I wrote the following method that will be used in activities and mainly in fragments (using inheritance). That method is supposed to allow me to never directly refer to the activity by calling //this or getActivity() The…
Maxime Claude
  • 965
  • 12
  • 27
12
votes
5 answers

How does the Garbage Collector decide when to kill objects held by WeakReferences?

I have an object, which I believe is held only by a WeakReference. I've traced its reference holders using SOS and SOSEX, and both confirm that this is the case (I'm not an SOS expert, so I could be wrong on this point). The standard explanation of…
Kennet Belenky
  • 2,755
  • 18
  • 20
12
votes
1 answer

Why does the initialization to `weak` return `nil` in Swift?

Why does the initialization to weak var return the variable as nil while the initialization to the usual var return the expected result? In the following code on ViewController.swift: weak var myButton: UIButton! var myButtonNotWeak:…
Blaszard
  • 30,954
  • 51
  • 153
  • 233
12
votes
3 answers

How to store weak reference object in array, dictionary in objc?

As we know, when you add an object to an array (NSMutableArray) or dictionary (NSMutableDictionary), it create a strong reference to the object. Is it possible to add an object to array with a weak reference to it?
user3313221
12
votes
4 answers

When does WeakReference#get() start returning null?

I want to use WeakReferences as part of an (android) bitmap-cache to be able to check when a bitmap isn't used anymore. My cache has a maximum-size that is smaller than the Java heap-space. When a new bitmap would overflow the cache, it should let…
Fabian Zeindl
  • 5,860
  • 8
  • 54
  • 78
12
votes
3 answers

C# GC.Collect not destroy an object if it's constructed using instance constructor initializer

Possible Duplicate: Resurrection difference in using Object Initializer I am having a hard time trying to understand how garbage collector works in C# (I'm using 2012, so c# 4.5). Here is my example code: public class A { public…
alex1234231
  • 193
  • 1
  • 6
12
votes
4 answers

Safely iterating over WeakKeyDictionary and WeakValueDictionary

The documentation of Python 3.2's weakref module's WeakKeyDictionary and WeakValueDictionary have a note on iterating over these containers: Note: Caution: Because a WeakKeyDictionary is built on top of a Python dictionary, it must not change size…
Feuermurmel
  • 9,490
  • 10
  • 60
  • 90
12
votes
3 answers

Weak NSString variable is not nil after setting the only strong reference to nil

I have a problem with this code : __strong NSString *yourString = @"Your String"; __weak NSString *myString = yourString; yourString = nil; __unsafe_unretained NSString *theirString = myString; NSLog(@"%p %@", yourString, yourString); NSLog(@"%p…
11
votes
3 answers

Are WeakHashMap cleared during a full GC?

I encountered some troubles with WeakHashMap. Consider this sample code: List list = new ArrayList(); Map map = new WeakHashMap(); String anObject = new String("string 1"); String anOtherObject =…
Slade
  • 133
  • 7
11
votes
2 answers

Objective-C - ARC - NSNumber - Segmentation Fault

I have an objective-C program and I am using ARC (Automatic Reference Counting), it throws a segmentation fault in line 23 (see program below). Question 1) Why does the segmentation fault occur ? Given below is the…
11
votes
2 answers

Indexable weak ordered set in Python

I was wondering if there is an easy way to build an indexable weak ordered set in Python. I tried to build one myself. Here's what I came up with: """ An indexable, ordered set of objects, which are held by weak reference. """ from nose.tools…
Neil G
  • 32,138
  • 39
  • 156
  • 257
11
votes
2 answers

Why weakref doesn't support built-in types in Python?

In Python weakref document( http://docs.python.org/library/weakref.html ), it says that Several built-in types such as list and dict do not directly support weak references but can add support through subclassing I think creating weakref for…
Ryan Ye
  • 3,159
  • 1
  • 22
  • 26