Class LeakDetector<T>
- java.lang.Object
-
- org.eclipse.jetty.util.component.AbstractLifeCycle
-
- org.eclipse.jetty.util.LeakDetector<T>
-
- Type Parameters:
T
- the resource type.
- All Implemented Interfaces:
java.lang.Runnable
,LifeCycle
public class LeakDetector<T> extends AbstractLifeCycle implements java.lang.Runnable
A facility to detect improper usage of resource pools.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 callacquired(Object)
every time the method to acquire a resource is called, andreleased(Object)
every time the method to release the resource is called.LeakDetector
keeps track of these resources and invokes methodleaked(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 toacquired(Object)
andreleased(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
usesPhantomReference
s to detect leaks.PhantomReference
s are enqueued in theirReferenceQueue
after they have been garbage collected (differently fromWeakReference
s 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. UsingPhantomReference
s is better than overridingObject.finalize()
and works also in those cases whereObject.finalize()
is not overridable.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
LeakDetector.LeakInfo
Information about the leak of a resource.-
Nested classes/interfaces inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
AbstractLifeCycle.AbstractLifeCycleListener
-
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.LifeCycle
LifeCycle.Listener
-
-
Constructor Summary
Constructors Constructor Description LeakDetector()
-
Method Summary
All Methods Instance Methods Concrete Methods 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 byLeakDetector
when it detects that a resource has been leaked.boolean
released(T resource)
Tracks the resource as been released.void
run()
-
Methods inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
addLifeCycleListener, getState, getState, getStopTimeout, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, setStopTimeout, start, stop, toString
-
-
-
-
Method Detail
-
acquired
public boolean acquired(T resource)
Tracks the resource as been acquired.- Parameters:
resource
- the resource that has been acquired- Returns:
- true whether the resource has been acquired normally, false if the resource has detected a leak (meaning that another acquire occurred before a release of the same resource)
- See Also:
released(Object)
-
released
public boolean released(T resource)
Tracks the resource as been released.- Parameters:
resource
- the resource that has been released- Returns:
- true whether the resource has been released normally (based on a previous acquire). false if the resource has been released without a prior acquire (such as a double release scenario)
- See Also:
acquired(Object)
-
id
public java.lang.String id(T resource)
Generates a unique ID for the given resource.- Parameters:
resource
- the resource to generate the unique ID for- Returns:
- the unique ID of the given resource
-
doStart
protected void doStart() throws java.lang.Exception
- Overrides:
doStart
in classAbstractLifeCycle
- Throws:
java.lang.Exception
-
doStop
protected void doStop() throws java.lang.Exception
- Overrides:
doStop
in classAbstractLifeCycle
- Throws:
java.lang.Exception
-
run
public void run()
- Specified by:
run
in interfacejava.lang.Runnable
-
leaked
protected void leaked(LeakDetector.LeakInfo leakInfo)
Callback method invoked byLeakDetector
when it detects that a resource has been leaked.- Parameters:
leakInfo
- the information about the leak
-
-