Package org.h2.util
Class CloseWatcher
A phantom reference to watch for unclosed objects.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate AutoCloseable
The closeable object.private String
The stack trace of when the object was created.private static final ReferenceQueue
<Object> The queue (might be set to null at any time).private static final Set
<CloseWatcher> The reference set. -
Constructor Summary
ConstructorsConstructorDescriptionCloseWatcher
(Object referent, ReferenceQueue<Object> q, AutoCloseable closeable) -
Method Summary
Modifier and TypeMethodDescriptionGet the open stack trace or null if none.static CloseWatcher
Check for an collected object.static CloseWatcher
register
(Object o, AutoCloseable closeable, boolean stackTrace) Register an object.static void
Unregister an object, so it is no longer tracked.Methods inherited from class java.lang.ref.PhantomReference
get
Methods inherited from class java.lang.ref.Reference
clear, clone, enqueue, isEnqueued, reachabilityFence, refersTo
-
Field Details
-
queue
The queue (might be set to null at any time). -
refs
The reference set. Must keep it, otherwise the references are garbage collected first and thus never enqueued. -
openStackTrace
The stack trace of when the object was created. It is converted to a string early on to avoid classloader problems (a classloader can't be garbage collected if there is a static reference to one of its classes). -
closeable
The closeable object.
-
-
Constructor Details
-
CloseWatcher
-
-
Method Details
-
pollUnclosed
Check for an collected object.- Returns:
- the first watcher
-
register
Register an object. Before calling this method, pollUnclosed() should be called in a loop to remove old references.- Parameters:
o
- the objectcloseable
- the object to closestackTrace
- whether the stack trace should be registered (this is relatively slow)- Returns:
- the close watcher
-
unregister
Unregister an object, so it is no longer tracked.- Parameters:
w
- the reference
-
getOpenStackTrace
Get the open stack trace or null if none.- Returns:
- the open stack trace
-
getCloseable
-