weak references : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (all)

weak reference
A weak reference is one that does not prevent the referenced object from being garbage collected. You might use them to manage a HashMap to look up a cache of objects. A weak reference is a reference that does not keep the object it refers to alive. A weak reference is not counted as a reference in garbage collection. If the object is not referred to elsewhere as well, it will be garbage collected.

The GC (Garbage Collection) will send some sort of finalize message to the object and then set any weakly-referencing variables to null whenever it disposes of the referenced object. This allows finalization logic to be run before the object is disposed of (e.g., close a file if still open, commit any open transaction(s), etc.). Java version 1.1 does not support weak references other than via an undocumented Ref class that is not supported under Netscape. Weak references arrived officially with JDK (Java Development Kit) 1.2. Java has three kinds of weak references, called soft references, weak references and phantom references, in order of increasing weakness.

Java has four orders of strength in holding onto Objects. In descending order from strongest to weakest they are:
  1. The JVM (Java Virtual Machine) holds onto regular Objects until they are no longer reachable by either clients or any container. In other words Objects are garbage collected when there are no more live references to them. Dead references don’t count.
  2. Soft references can be deleted from a container if the clients are no longer referencing them and memory is tight.
  3. Weak references are automatically deleted from a container as soon clients stop referencing them.
  4. Phantom references point to objects that are already dead and have been finalised.
Soft vs Weak vs Phantom References
Type Purpose Use When GCed Implementing Class
Strong Reference An ordinary reference. Keeps objects alive as long as they are referenced. normal reference. Any object not pointed to can be reclaimed. default
Soft Reference Keeps objects alive provided there’s enough memory. to keep objects alive even after clients have removed their references (memory-sensitive caches), in case clients start asking for them again by key. After a first gc pass, the JVM decides it still needs to reclaim more space. java.lang.ref.SoftReference
Weak Reference Keeps objects alive only while they’re in use (reachable) by clients. Containers that automatically delete objects no longer in use. After gc determines the object is only weakly reachable java.lang.ref.WeakReference
java.util.WeakHashMap
Phantom Reference Lets you clean up after finalization but before the space is reclaimed (replaces or augments the use of finalize()) Special clean up processing After finalization. java.lang.ref.PhantomReference

Learning More

Oracle’s Javadoc on WeakHashMap class : available:

This page is posted
on the web at:

http://mindprod.com/jgloss/weak.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\weak.html
Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

IP:[65.110.21.43]
Your face IP:[3.144.42.196]
You are visitor number