Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Weak reference
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Variations == Some languages have multiple levels of weak reference strength. For example, [[Java (programming language)|Java]] has, in order of decreasing strength, [[soft reference|soft]], weak, and [[phantom reference|phantom]] references, defined in the [[Java package|package]] [[Java Platform, Standard Edition#java.lang.ref|java.lang.ref]].<ref>{{cite web |last=Nicholas |first=Ethan |url=http://weblogs.java.net/blog/2006/05/04/understanding-weak-references |title=Understanding Weak References |work=java.net |date=May 4, 2006 |archive-url=https://web.archive.org/web/20110303225354/http://weblogs.java.net/blog/2006/05/04/understanding-weak-references |access-date=October 1, 2010|archive-date=2011-03-03 }}</ref> Each reference type has an associated notion of reachability. The garbage collector (GC) uses an object's type of reachability to determine when to free the object. It is safe for the GC to free an object that is softly reachable, but the GC may decide not to do so if it believes the JVM can spare the memory (e.g. the JVM has much unused heap space). The GC will free a weakly reachable object as soon as the GC notices the object. Unlike the other reference types, a phantom reference cannot be followed. On the other hand, phantom references provide a mechanism to notify the program when an object has been freed (notification is implemented using ReferenceQueues).<!-- what situations call for which type of reference --> In C#, weak references are distinguished by whether they track [[object resurrection]] or not. This distinction does not occur for strong references, as objects are not [[finalization|finalized]] if they have any strong references to them. By default, in C# weak reference do not track resurrection, meaning a weak reference is not updated if an object is resurrected; these are called '''short weak references''', and weak references that track resurrection are called '''long weak references'''.{{sfn|Goldshtein|Zurbalev|Flatow|2012|p=[https://books.google.com/books?id=D3J58cs-i44C&q=resurrection&pg=PA131 131]}} Some non-garbage-collected languages, such as [[C++]], provide weak/strong reference functionality as part of supporting garbage collection libraries. The Boost C++ library provides strong and weak references. It is a mistake to use regular C++ pointers as the ''weak'' counterparts of [[smart pointer]]s because such usage removes the ability to detect when the ''strong'' reference count has gone to 0 and the object has been deleted. Worse yet, it does not allow for detection of whether another strong reference is already tracking a given plain pointer. This introduces the possibility of having two (or more) smart pointers tracking the same plain pointer (which causes corruption as soon as one of these smart pointers' reference count reaches 0 and the object gets deleted).
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)