![]() |
The Java Developers Almanac 1.4 |
|
e107. Determining When an Object Is No Longer UsedA weak reference is used to determine when an object is no longer being referenced. // Create the weak reference.
ReferenceQueue rq = new ReferenceQueue();
WeakReference wr = new WeakReference(object, rq);
// Wait for all the references to the object.
try {
while (true) {
Reference r = rq.remove();
if (r == wr) {
// Object is no longer referenced.
}
}
} catch (InterruptedException e) {
}
e108. Determining When an Object Will Be Reclaimed © 2002 Addison-Wesley. |