Class ResourceLeakDetector.DefaultResourceLeak<T>
- All Implemented Interfaces:
ResourceLeak, ResourceLeakTracker<T>
- Enclosing class:
ResourceLeakDetector<T>
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final java.util.Set<ResourceLeakDetector.DefaultResourceLeak<?>> private intprivate static final java.util.concurrent.atomic.AtomicIntegerFieldUpdater<ResourceLeakDetector.DefaultResourceLeak<?>> private ResourceLeakDetector.TraceRecordprivate static final java.util.concurrent.atomic.AtomicReferenceFieldUpdater<ResourceLeakDetector.DefaultResourceLeak<?>, ResourceLeakDetector.TraceRecord> private final int -
Constructor Summary
ConstructorsConstructorDescriptionDefaultResourceLeak(java.lang.Object referent, java.lang.ref.ReferenceQueue<java.lang.Object> refQueue, java.util.Set<ResourceLeakDetector.DefaultResourceLeak<?>> allLeaks, java.lang.Object initialHint) -
Method Summary
Modifier and TypeMethodDescriptionbooleanclose()Close the leak so thatResourceLeakDetectordoes not warn about leaked resources.booleanClose the leak so thatResourceLeakTrackerdoes not warn about leaked resources.(package private) booleandispose()private java.lang.Stringjava.lang.ThrowableGet aThrowablerepresenting the stack trace of the originalResourceLeakTracker.close(Object)call.(package private) java.lang.Stringprivate static voidreachabilityFence0(java.lang.Object ref) Ensures that the object referenced by the given reference remains strongly reachable, regardless of any prior actions of the program that might otherwise cause the object to become unreachable; thus, the referenced object is not reclaimable by garbage collection at least until after the invocation of this method.voidrecord()Records the caller's current stack trace so that theResourceLeakDetectorcan tell where the leaked resource was accessed lastly.voidrecord(java.lang.Object hint) Records the caller's current stack trace and the specified additional arbitrary information so that theResourceLeakDetectorcan tell where the leaked resource was accessed lastly.private voidrecord0(java.lang.Object hint) This method works by exponentially backing off as more records are present in the stack.java.lang.StringtoString()Methods inherited from class java.lang.ref.Reference
clear, enqueue, get, isEnqueuedMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Field Details
-
headUpdater
private static final java.util.concurrent.atomic.AtomicReferenceFieldUpdater<ResourceLeakDetector.DefaultResourceLeak<?>, ResourceLeakDetector.TraceRecord> headUpdater -
droppedRecordsUpdater
private static final java.util.concurrent.atomic.AtomicIntegerFieldUpdater<ResourceLeakDetector.DefaultResourceLeak<?>> droppedRecordsUpdater -
head
-
droppedRecords
private volatile int droppedRecords -
allLeaks
-
trackedHash
private final int trackedHash
-
-
Constructor Details
-
DefaultResourceLeak
DefaultResourceLeak(java.lang.Object referent, java.lang.ref.ReferenceQueue<java.lang.Object> refQueue, java.util.Set<ResourceLeakDetector.DefaultResourceLeak<?>> allLeaks, java.lang.Object initialHint)
-
-
Method Details
-
record
public void record()Description copied from interface:ResourceLeakTrackerRecords the caller's current stack trace so that theResourceLeakDetectorcan tell where the leaked resource was accessed lastly. This method is a shortcut torecord(null).- Specified by:
recordin interfaceResourceLeak- Specified by:
recordin interfaceResourceLeakTracker<T>
-
record
public void record(java.lang.Object hint) Description copied from interface:ResourceLeakTrackerRecords the caller's current stack trace and the specified additional arbitrary information so that theResourceLeakDetectorcan tell where the leaked resource was accessed lastly.- Specified by:
recordin interfaceResourceLeak- Specified by:
recordin interfaceResourceLeakTracker<T>
-
record0
private void record0(java.lang.Object hint) This method works by exponentially backing off as more records are present in the stack. Each record has a 1 / 2^n chance of dropping the top most record and replacing it with itself. This has a number of convenient properties:- The current record is always recorded. This is due to the compare and swap dropping the top most record, rather than the to-be-pushed record.
- The very last access will always be recorded. This comes as a property of 1.
- It is possible to retain more records than the target, based upon the probability distribution.
- It is easy to keep a precise record of the number of elements in the stack, since each element has to know how tall the stack is.
ResourceLeakDetector.TARGET_RECORDSaccesses, backoff occurs. This matches typical access patterns, where there are either a high number of accesses (i.e. a cached buffer), or low (an ephemeral buffer), but not many in between.The use of atomics avoids serializing a high number of accesses, when most of the records will be thrown away. High contention only happens when there are very few existing records, which is only likely when the object isn't shared! If this is a problem, the loop can be aborted and the record dropped, because another thread won the race.
-
dispose
boolean dispose() -
close
public boolean close()Description copied from interface:ResourceLeakClose the leak so thatResourceLeakDetectordoes not warn about leaked resources.- Specified by:
closein interfaceResourceLeak- Returns:
trueif called first time,falseif called already
-
close
Description copied from interface:ResourceLeakTrackerClose the leak so thatResourceLeakTrackerdoes not warn about leaked resources. After this method is called a leak associated with this ResourceLeakTracker should not be reported.- Specified by:
closein interfaceResourceLeakTracker<T>- Returns:
trueif called first time,falseif called already
-
reachabilityFence0
private static void reachabilityFence0(java.lang.Object ref) Ensures that the object referenced by the given reference remains strongly reachable, regardless of any prior actions of the program that might otherwise cause the object to become unreachable; thus, the referenced object is not reclaimable by garbage collection at least until after the invocation of this method.Recent versions of the JDK have a nasty habit of prematurely deciding objects are unreachable. see: https://stackoverflow.com/questions/26642153/finalize-called-on-strongly-reachable-object-in-java-8 The Java 9 method Reference.reachabilityFence offers a solution to this problem.
This method is always implemented as a synchronization on
ref, not asReference.reachabilityFencefor consistency across platforms and to allow building on JDK 6-8. It is the caller's responsibility to ensure that this synchronization will not cause deadlock.- Parameters:
ref- the reference. Ifnull, this method has no effect.- See Also:
-
getCloseStackTraceIfAny
public java.lang.Throwable getCloseStackTraceIfAny()Description copied from interface:ResourceLeakTrackerGet aThrowablerepresenting the stack trace of the originalResourceLeakTracker.close(Object)call. If this tracker hasn't been cloesd, or close tracking isn't supported or enabled, then this method returnsnull.- Specified by:
getCloseStackTraceIfAnyin interfaceResourceLeakTracker<T>- Returns:
- A throwable with the stack trace of the successful close call, or
null.
-
toString
public java.lang.String toString()- Overrides:
toStringin classjava.lang.Object
-
getReportAndClearRecords
java.lang.String getReportAndClearRecords() -
generateReport
-