Determining When an Object Is No Longer Used

A 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) { }

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.