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 java, c#, python, perl and lisp.
Reference counting languages, such as objective-c and swift, 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.