CleanupRegistry

  • CleanupRegistry manages resource cleanup by tracking objects and executing cleanup logic when they are garbage collected.

  • It utilizes a ReferenceQueue to monitor garbage collection and a Runnable to perform cleanup tasks.

  • CleanupRegistry allows registering objects for tracking and provides methods for manual resource release and reclaiming released resources.

  • The register method associates a cleanup callback (Runnable) with an object, ensuring the callback is executed upon garbage collection.

public class CleanupRegistry

Maintains a ReferenceQueue and executes a Runnable after each object in the queue is garbage collected.

Public Constructors

CleanupRegistry(HashSet<CleanupItem<T>> cleanupItemHashSet, ReferenceQueue<T> referenceQueue)

Public Methods

void
destroyAllResources()
Ignores reference count and releases any associated resources
long
reclaimReleasedResources()
Polls the ReferenceQueue for garbage collected objects and runs the associated Runnable
void
register(T trackedObject, Runnable cleanupCallback)
Adds trackedOBject to the ReferenceQueue.

Inherited Methods

Public Constructors

public CleanupRegistry ()

public CleanupRegistry (HashSet<CleanupItem<T>> cleanupItemHashSet, ReferenceQueue<T> referenceQueue)

Parameters
cleanupItemHashSet
referenceQueue

Public Methods

public void destroyAllResources ()

Ignores reference count and releases any associated resources

public long reclaimReleasedResources ()

Polls the ReferenceQueue for garbage collected objects and runs the associated Runnable

Returns
  • count of resources remaining.

public void register (T trackedObject, Runnable cleanupCallback)

Adds trackedOBject to the ReferenceQueue.

Parameters
trackedObject The target to be tracked.
cleanupCallback Will be called after trackedOBject is disposed.