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

Trying to understand Microsoft's implementation of WeakReference

As a seasoned C++ programmer trying to get accustomed to .NET, there's an implementation detail in Microsoft's WeakReference "Target" property that's bugging me... public class WeakReference : ISerializable { internal IntPtr m_handle; …
Kevin
  • 1,120
  • 6
  • 13
10
votes
2 answers

Weakreference get() method how safe is it? (Android, asynctask)

I am making an Android mobile app. I have a WeakReference to my Activity in the AsyncTask to ensure that it can be garbage collected. When onPostExecute() gets called, I do Acitivty activity = mWeakRef.get(); Then I use the activity object to…
dnkoutso
  • 6,041
  • 4
  • 37
  • 58
10
votes
2 answers

How to use WeakRef in Typescript?

I want to use WeakRef in Typescript. I tried using the last version available at the moment (4.1.5). I have a compilation error: const toIdString = (o: Object): string => o.constructor.name + JSON.stringify(o); export class MemoCache { static…
jlguenego
  • 1,192
  • 15
  • 23
10
votes
2 answers

OutOfMemoryException despite using WeakHashMap

If do not call System.gc(), the system will throw an OutOfMemoryException. I do not know why I need to call System.gc() explicitly; the JVM should call gc() itself, right? Please advise. The following is my test code: public static void…
10
votes
1 answer

Swift Weak Reference Much Slower than Strong Reference

I'm building a physics engine in Swift. After making some recent additions to the engine and running the benchmarking tests I noticed the performance was drastically slower. For example, in the screenshots below you can see how the FPS dropped from…
10
votes
4 answers

What's the state of a weak reference that has been manually enqueued?

What's the state of an object when you manually enqueue a reference? this.s = "foo"; WeakReference wr = new WeakReference(this.s); wr.enqueue(); All the documentation I've found talks about the garbage collector enqueueing the…
James Moore
  • 8,636
  • 5
  • 71
  • 90
10
votes
3 answers

In Perl, why does copying a weak reference create a normal, strong, reference?

Scalar::Util::weaken says: NOTE: Copying a weak reference creates a normal, strong, reference. I can't understand why Perl handle it this way. In my applications, I use weaken to break cycles. Sometimes i have to weaken references that would be…
key_
  • 577
  • 4
  • 15
10
votes
1 answer

How does __del__() interfere with garbage collection?

I read an example in David Beazley's Python Essential Reference: class Account(object): def __init__(self,name,balance): self.name = name self.balance = balance self.observers = set() def __del__(self): …
10
votes
1 answer

How can I lower the weak ref processing time during GC?

Currently I am facing the problem that my application is showing long GC times sporadically, but all these are only caused by weak reference processing. So the thread stopped time is always close to the weak ref processing time. All other GC cycles…
ReneS
  • 3,535
  • 2
  • 26
  • 35
10
votes
3 answers

Android: the GC doesn't respect SoftReferences?

It seams that Dalvik's garbage collector doesn't respect SoftReferences and removes them as soon as possible, just like WeakReferences. I'm not 100% sure yet, but despite the fact that there is still ~3MB of free memory my SoftReferences get cleared…
JBM
  • 2,930
  • 2
  • 24
  • 28
10
votes
1 answer

How to build an infinite tree with duplicate elimination via cache of weak pointers in Haskell

The following code builds up an infinite tree, while at the same time creating a cache of all subtrees, such that no duplicate subtrees are created. The rationale for elimination of duplicate subtrees comes from the application to state trees of…
hkBst
  • 2,818
  • 10
  • 29
10
votes
5 answers

How to create weak reference in Objective-C?

i have a situation like this: NSMutableArray * A = [[NSMutableArray alloc]initwithObjects:@"one",nil]; NSMutableArray * B = [[NSMutableArray alloc]initwithObjects:@"two",nil]; [A addObject:B]; [B addObject:A]; now here is a retain cycle, How can i…
Matrix
  • 7,477
  • 14
  • 66
  • 97
10
votes
6 answers

Why is a WeakReference useless in a destructor?

Consider the following code: class Program { static void Main(string[] args) { A a = new A(); CreateB(a); GC.Collect(); GC.WaitForPendingFinalizers(); Console.WriteLine("And here's:" + a); …
Bubblewrap
  • 7,266
  • 1
  • 35
  • 33
10
votes
3 answers

How to do compare and increment atomically?

In my attempt to develope a thread-safe C++ weak pointer template class, I need to check a flag that indicating the object is still alive, if yes then increment the object's reference count and I need to do the both steps atomically. I know the…
10
votes
3 answers

android - java - WeakReferences with an ArrayList?

I know that with a WeakReference, if I make a WeakReference to something that unless there's a direct reference to it that it will be Garbage Collected with the next GC cycle. My question becomes, what if I make an ArrayList of WeakReferences? For…
codingNewb
  • 470
  • 1
  • 8
  • 23