Friday 1 July 2011

Determining When an Object Is No Longer Used


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

People who read this post also read :



No comments:

Post a Comment