programming-examples/java/Core_Java/Determining When an Object Is No Longer Used.java
2019-11-15 12:59:38 +01:00

17 lines
438 B
Java

Determining When an Object Is No Longer Used
// 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) {
}