T - the resource type.java.lang.Runnable, LifeCyclepublic class LeakDetector<T> extends AbstractLifeCycle implements java.lang.Runnable
Resource pools usually have a method to acquire a pooled resource and a method to released it back to the pool.
To detect if client code acquires a resource but never releases it, the resource pool can be modified to use a
LeakDetector. The modified resource pool should call acquired(Object) every time the method to
acquire a resource is called, and released(Object) every time the method to release the resource is called.
LeakDetector keeps track of these resources and invokes method
leaked(org.eclipse.jetty.util.LeakDetector.LeakInfo) when it detects that a resource has been leaked (that
is, acquired but never released).
To detect whether client code releases a resource without having acquired it, the resource pool can be modified to
check the return value of released(Object): if false, it means that the resource was not acquired.
IMPLEMENTATION NOTES
This class relies on System.identityHashCode(Object) to create a unique id for each resource passed to
acquired(Object) and released(Object). System.identityHashCode(Object) does not guarantee
that it will not generate the same number for different objects, but in practice the chance of collision is rare.
LeakDetector uses PhantomReferences to detect leaks. PhantomReferences are enqueued in their
ReferenceQueue after they have been garbage collected (differently from WeakReferences that
are enqueued before). Since the resource is now garbage collected, LeakDetector checks whether it
has been released and if not, it reports a leak. Using PhantomReferences is better than overriding
Object.finalize() and works also in those cases where Object.finalize() is not overridable.
| Modifier and Type | Class | Description |
|---|---|---|
class |
LeakDetector.LeakInfo |
Information about the leak of a resource.
|
AbstractLifeCycle.AbstractLifeCycleListenerLifeCycle.ListenerFAILED, RUNNING, STARTED, STARTING, STOP_ON_FAILURE, STOPPED, STOPPING| Constructor | Description |
|---|---|
LeakDetector() |
| Modifier and Type | Method | Description |
|---|---|---|
boolean |
acquired(T resource) |
Tracks the resource as been acquired.
|
protected void |
doStart() |
|
protected void |
doStop() |
|
java.lang.String |
id(T resource) |
Generates a unique ID for the given resource.
|
protected void |
leaked(LeakDetector.LeakInfo leakInfo) |
Callback method invoked by
LeakDetector when it detects that a resource has been leaked. |
boolean |
released(T resource) |
Tracks the resource as been released.
|
void |
run() |
addLifeCycleListener, getState, getState, getStopTimeout, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, setStopTimeout, start, stoppublic boolean acquired(T resource)
resource - the resource that has been acquiredreleased(Object)public boolean released(T resource)
resource - the resource that has been releasedacquired(Object)public java.lang.String id(T resource)
resource - the resource to generate the unique ID forprotected void doStart()
throws java.lang.Exception
doStart in class AbstractLifeCyclejava.lang.Exceptionprotected void doStop()
throws java.lang.Exception
doStop in class AbstractLifeCyclejava.lang.Exceptionpublic void run()
run in interface java.lang.Runnableprotected void leaked(LeakDetector.LeakInfo leakInfo)
LeakDetector when it detects that a resource has been leaked.leakInfo - the information about the leakCopyright © 1995–2017 Webtide. All rights reserved.