class Ref::ReferenceQueue

This class provides a simple thread safe container to hold a reference queue. Instances of WeakReference can be added to the queue and as the objects pointed to by those references are cleaned up by the garbage collector, the references will be added to the queue.

The reason for using a reference queue is that it tends to be more efficient than adding individual finalizers to objects and the cleanup code can be handled by a thread outside of garbage collection.

In general, you should create your own subclass of WeakReference that contains the logic needed to complete the cleanup. The object pointed to will have already been cleaned up and the reference cannot maintain a reference to the object.

Example usage:

class MyRef < Ref::WeakReference
  def cleanup
    # Do something...
  end
end

queue = Ref::ReferenceQueue.new
ref = MyRef.new(Object.new)
queue.monitor(ref)
queue.shift                 # = nil
ObjectSpace.garbage_collect
r = queue.shift             # = ref
r.cleanup