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!
===Java=== Java 1.2 in 1998 introduced<ref>{{cite web|url=http://docs.oracle.com/javase/7/docs/api/java/lang/ref/WeakReference.html|title=WeakReference (Java Platform SE 7 )|website=docs.oracle.com}}</ref> two kinds of weak references, one known as a "soft reference" (intended to be used for maintaining GC-managed in-memory caches, but which doesn't work very well in practice on some platforms with dynamic heap like Android<ref>{{cite web|url=https://developer.android.com/reference/java/lang/ref/SoftReference.html|title=SoftReference - Android Developers|website=developer.android.com}}</ref>) and the other simply as a "weak reference". It also added a related experimental mechanism dubbed "phantom references" as an alternative to the dangerous and inefficient finalize() mechanism.<ref>{{cite web|url=http://docs.oracle.com/javase/7/docs/api/java/lang/ref/PhantomReference.html|title=PhantomReference (Java Platform SE 7 )|website=docs.oracle.com}}</ref> If a weak reference is created, and then elsewhere in the code <code>get()</code> is used to get the actual object, the weak reference is not strong enough to prevent garbage collection, so it may be (if there are no strong references to the object) that <code>get()</code> suddenly starts returning null.<ref>https://web.archive.org/web/20110303225354/http://weblogs.java.net/blog/2006/05/04/understanding-weak-references Java Examples</ref> <syntaxhighlight lang=java> import java.lang.ref.WeakReference; public class ReferenceTest { public static void main(String[] args) throws InterruptedException { WeakReference r = new WeakReference("I'm here"); StrongReference sr = new StrongReference("I'm here"); System.out.println("Before gc: r=" + r.get() + ", static=" + sr.get()); System.gc(); Thread.sleep(100); // Only r.get() becomes null. System.out.println("After gc: r=" + r.get() + ", static=" + sr.get()); } } </syntaxhighlight> Another use of weak references is in writing a [[cache (computing)|cache]]. Using, for example, a weak [[hash map]], one can store in the cache the various referred objects via a weak reference. When the garbage collector runs — when for example the application's memory usage gets sufficiently high — those cached objects which are no longer directly referenced by other objects are removed from the cache.
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)