Class LeakDetect

java.lang.Object
com.linkedin.alpini.base.misc.LeakDetect

public final class LeakDetect extends Object
General purpose object reference leak detector with recovery.

A reference to the phantom must still be maintained somewhere... For example: private final AtomicReference<PhantomReference<AsyncFutureListener<?>>> _phantom = new AtomicReference<>();

Then using the LeakDetect tool is as simple as: PhantomReference<AsyncFutureListener<?>> phantom = LeakDetect .newReference(responseListener, () -> ctx.executor().execute(() -> promise.setSuccess(internalBuildErrorResponse(request, new IllegalStateException("Reference lost"))))); promise.addListener(future -> { phantom.clear(); _phantom.compareAndSet(phantom, null); }); _phantom.lazySet(phantom);

Note that the recovery should not reference any object which may hold a reference to the object which is being monitored for leaks.

  • Method Details

    • newReference

      public static <T> PhantomReference<T> newReference(T object, Runnable recover)
      Make a phantom reference for leak detection. Be sure to call Reference.clear() when the object is no longer considered leaked.
      Parameters:
      object - Object to monitor.
      recover - Task to invoke when a leak occurs.
      Returns:
      Phantom reference.