|
|
|
| |
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 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 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 1.2. Java has three kinds of weak references,
called soft references, weak references, and
phantom references, in order of increasing weakness.
• Soft references are garbage collected only
when memory is low.
• Weak references are garbage collected on a
gc even when memory is abundant.
• Phantom references point to objects that are
dead and have been finalised.
|
|