Class LeakDetector<T>
- Type Parameters:
- T- the resource type.
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.
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionclassInformation about the leak of a resource.Nested classes/interfaces inherited from class org.eclipse.jetty.util.component.AbstractLifeCycleAbstractLifeCycle.AbstractLifeCycleListener, AbstractLifeCycle.StopExceptionNested classes/interfaces inherited from interface org.eclipse.jetty.util.component.LifeCycleLifeCycle.Listener
- 
Field Summary
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionbooleanTracks the resource as been acquired.protected voiddoStart()Method to override to start the lifecycleprotected voiddoStop()Method to override to stop the lifecycleGenerates a unique ID for the given resource.protected voidleaked(LeakDetector<T>.LeakInfo leakInfo) Callback method invoked byLeakDetectorwhen it detects that a resource has been leaked.booleanTracks the resource as been released.voidrun()Methods inherited from class org.eclipse.jetty.util.component.AbstractLifeCycleaddEventListener, getEventListeners, getState, getState, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeEventListener, setEventListeners, start, stop, toString
- 
Constructor Details- 
LeakDetectorpublic LeakDetector()
 
- 
- 
Method Details- 
acquiredTracks 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:
 
- 
releasedTracks 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:
 
- 
idGenerates 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
 
- 
doStartDescription copied from class:AbstractLifeCycleMethod to override to start the lifecycle- Overrides:
- doStartin class- AbstractLifeCycle
- Throws:
- AbstractLifeCycle.StopException- If thrown, the lifecycle will immediately be stopped.
- Exception- If there was a problem starting. Will cause a transition to FAILED state
 
- 
doStopDescription copied from class:AbstractLifeCycleMethod to override to stop the lifecycle- Overrides:
- doStopin class- AbstractLifeCycle
- Throws:
- Exception- If there was a problem stopping. Will cause a transition to FAILED state
 
- 
runpublic void run()
- 
leakedCallback method invoked byLeakDetectorwhen it detects that a resource has been leaked.- Parameters:
- leakInfo- the information about the leak
 
 
-