Top Inners Fields Constructors Methods
io.netty.util

public Class ResourceLeakDetector<T>

extends Object
Class Inheritance
Imports
io.netty.util.internal.EmptyArrays, .ObjectUtil, .PlatformDependent, .SystemPropertyUtil, io.netty.util.internal.logging.InternalLogger, .InternalLoggerFactory, java.lang.ref.WeakReference, .ReferenceQueue, java.lang.reflect.Method, java.util.Arrays, .Collections, .HashSet, .Set, java.util.concurrent.ConcurrentHashMap, java.util.concurrent.atomic.AtomicIntegerFieldUpdater, .AtomicReference, .AtomicReferenceFieldUpdater

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
ResourceLeakDetector.DefaultResourceLeak<T>

Deprecated as a consequence of ResourceLeak being deprecated.
public static interface
public static enum
ResourceLeakDetector.Level

Represents the level of resource leak detection.

private static class

Field Summary

Modifier and TypeField and Description
private final Set<ResourceLeakDetector.DefaultResourceLeak<?>>
allLeaks

the collection of active resources

private static final ResourceLeakDetector.Level
private static final int
private static final int
private static final AtomicReference<String[]>
private volatile ResourceLeakDetector.LeakListener
leakListener

Will be notified once a leak is detected.

private static ResourceLeakDetector.Level
private static final InternalLogger
private static final String
private static final String
private static final String
private static final String
private final ReferenceQueue<Object>
private final Set<String>
private final String
pack-priv static final int
private final int
private static final int

Constructor Summary

AccessConstructor and Description
public
public
public
ResourceLeakDetector(Class<?> resourceType, int samplingInterval, long
This is deprecated and will be ignored.
maxActive
)

public
ResourceLeakDetector(Class<?> resourceType, int samplingInterval)

This should not be used directly by users of ResourceLeakDetector.

public
ResourceLeakDetector(String resourceType, int samplingInterval, long
This is deprecated and will be ignored.
maxActive
)

Method Summary

Modifier and TypeMethod and Description
public static void
addExclusions(Class<T> clz, String... methodNames)

private void
protected Object
getInitialHint(String resourceType)

Create a hint object to be attached to an object tracked by this record.

public static ResourceLeakDetector.Level
getLevel()

Returns the current resource leak detection level.

public static boolean
isEnabled()

Returns true if resource leak detection is enabled.

protected boolean

Returns:

true to enable leak reporting.
needReport
()

When the return value is true, reportTracedLeak and reportUntracedLeak will be called once a leak is detected, otherwise not.

public final ResourceLeak

Returns:

the ResourceLeak or null
open
(T obj)

Deprecated use track(Object)
Creates a new ResourceLeak which is expected to be closed via ResourceLeak#close() when the related resource is deallocated.
protected void
reportInstancesLeak(String resourceType)

Deprecated This method will no longer be invoked by ResourceLeakDetector.
private void
protected void
reportTracedLeak(String resourceType, String records)

This method is called when a traced leak is detected.

protected void
reportUntracedLeak(String resourceType)

This method is called when an untraced leak is detected.

public static void
setEnabled(boolean enabled)

Deprecated Use setLevel(Level) instead.
public void
setLeakListener(ResourceLeakDetector.LeakListener leakListener)

Set leak listener.

public static void
setLevel(ResourceLeakDetector.Level level)

Sets the resource leak detection level.

public final ResourceLeakTracker<T>

Returns:

the ResourceLeakTracker or null
track
(T obj)

Creates a new ResourceLeakTracker which is expected to be closed via ResourceLeakTracker#close(Object) when the related resource is deallocated.

private ResourceLeakDetector.DefaultResourceLeak<T>
track0(T obj, boolean force)

Deprecated as a consequence of ResourceLeakDetector.DefaultResourceLeak being deprecated.
public ResourceLeakTracker<T>

Returns:

the ResourceLeakTracker
trackForcibly
(T obj)

Creates a new ResourceLeakTracker which is expected to be closed via ResourceLeakTracker#close(Object) when the related resource is deallocated.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

allLeaksback to summary
private final Set<ResourceLeakDetector.DefaultResourceLeak<?>> allLeaks

the collection of active resources

DEFAULT_LEVELback to summary
private static final ResourceLeakDetector.Level DEFAULT_LEVEL
DEFAULT_SAMPLING_INTERVALback to summary
private static final int DEFAULT_SAMPLING_INTERVAL
DEFAULT_TARGET_RECORDSback to summary
private static final int DEFAULT_TARGET_RECORDS
excludedMethodsback to summary
private static final AtomicReference<String[]> excludedMethods
leakListenerback to summary
private volatile ResourceLeakDetector.LeakListener leakListener

Will be notified once a leak is detected.

levelback to summary
private static ResourceLeakDetector.Level level
loggerback to summary
private static final InternalLogger logger
PROP_LEVELback to summary
private static final String PROP_LEVEL
PROP_LEVEL_OLDback to summary
private static final String PROP_LEVEL_OLD
PROP_SAMPLING_INTERVALback to summary
private static final String PROP_SAMPLING_INTERVAL
PROP_TARGET_RECORDSback to summary
private static final String PROP_TARGET_RECORDS
refQueueback to summary
private final ReferenceQueue<Object> refQueue
reportedLeaksback to summary
private final Set<String> reportedLeaks
resourceTypeback to summary
private final String resourceType
SAMPLING_INTERVALback to summary
pack-priv static final int SAMPLING_INTERVAL
samplingIntervalback to summary
private final int samplingInterval
TARGET_RECORDSback to summary
private static final int TARGET_RECORDS

Constructor Detail

ResourceLeakDetectorback to summary
public ResourceLeakDetector(Class<?> resourceType)
Annotations
@Deprecated
ResourceLeakDetectorback to summary
public ResourceLeakDetector(String resourceType)
Annotations
@Deprecated
ResourceLeakDetectorback to summary
public ResourceLeakDetector(Class<?> resourceType, int samplingInterval, long maxActive)
Parameters
maxActive:long

This is deprecated and will be ignored.

Annotations
@Deprecated
ResourceLeakDetectorback to summary
public ResourceLeakDetector(Class<?> resourceType, int samplingInterval)

This should not be used directly by users of ResourceLeakDetector. Please use ResourceLeakDetectorFactory#newResourceLeakDetector(Class) or ResourceLeakDetectorFactory#newResourceLeakDetector(Class, int, long)

Annotations
@SuppressWarnings:deprecation
ResourceLeakDetectorback to summary
public ResourceLeakDetector(String resourceType, int samplingInterval, long maxActive)
Parameters
maxActive:long

This is deprecated and will be ignored.

Annotations
@Deprecated

Method Detail

addExclusionsback to summary
public static void addExclusions(Class<T> clz, String... methodNames)
clearRefQueueback to summary
private void clearRefQueue()
getInitialHintback to summary
protected Object getInitialHint(String resourceType)

Create a hint object to be attached to an object tracked by this record. Similar to the additional information supplied to ResourceLeakTracker#record(Object), will be printed alongside the stack trace of the creation of the resource.

getLevelback to summary
public static ResourceLeakDetector.Level getLevel()

Returns the current resource leak detection level.

isEnabledback to summary
public static boolean isEnabled()

Returns true if resource leak detection is enabled.

needReportback to summary
protected boolean needReport()

When the return value is true, reportTracedLeak and reportUntracedLeak will be called once a leak is detected, otherwise not.

Returns:boolean

true to enable leak reporting.

openback to summary
public final ResourceLeak open(T obj)

Deprecated

use track(Object)

Creates a new ResourceLeak which is expected to be closed via ResourceLeak#close() when the related resource is deallocated.

Returns:ResourceLeak

the ResourceLeak or null

Annotations
@Deprecated
reportInstancesLeakback to summary
protected void reportInstancesLeak(String resourceType)

Deprecated

This method will no longer be invoked by ResourceLeakDetector.

Annotations
@Deprecated
reportLeakback to summary
private void reportLeak()
reportTracedLeakback to summary
protected void reportTracedLeak(String resourceType, String records)

This method is called when a traced leak is detected. It can be overridden for tracking how many times leaks have been detected.

reportUntracedLeakback to summary
protected void reportUntracedLeak(String resourceType)

This method is called when an untraced leak is detected. It can be overridden for tracking how many times leaks have been detected.

setEnabledback to summary
public static void setEnabled(boolean enabled)

Deprecated

Use setLevel(Level) instead.

Annotations
@Deprecated
setLeakListenerback to summary
public void setLeakListener(ResourceLeakDetector.LeakListener leakListener)

Set leak listener. Previous listener will be replaced.

setLevelback to summary
public static void setLevel(ResourceLeakDetector.Level level)

Sets the resource leak detection level.

trackback to summary
public final ResourceLeakTracker<T> track(T obj)

Creates a new ResourceLeakTracker which is expected to be closed via ResourceLeakTracker#close(Object) when the related resource is deallocated.

Returns:ResourceLeakTracker<T>

the ResourceLeakTracker or null

Annotations
@SuppressWarnings:unchecked
track0back to summary
private ResourceLeakDetector.DefaultResourceLeak<T> track0(T obj, boolean force)

Deprecated

as a consequence of ResourceLeakDetector.DefaultResourceLeak being deprecated. See corresponding docs for further information.

Annotations
@SuppressWarnings:unchecked
trackForciblyback to summary
public ResourceLeakTracker<T> trackForcibly(T obj)

Creates a new ResourceLeakTracker which is expected to be closed via ResourceLeakTracker#close(Object) when the related resource is deallocated. Unlike track(Object), this method always returns a tracker, regardless of the detection settings.

Returns:ResourceLeakTracker<T>

the ResourceLeakTracker

Annotations
@SuppressWarnings:unchecked
io.netty.util back to summary

private final Class ResourceLeakDetector.DefaultResourceLeak<T>

extends WeakReference<Object>
implements ResourceLeakTracker<T>, ResourceLeak
Class Inheritance
All Implemented Interfaces
io.netty.util.ResourceLeak, io.netty.util.ResourceLeakTracker
Annotations
@SuppressWarnings:deprecation

Deprecated

as a consequence of ResourceLeak being deprecated. See corresponding docs for further information.

Field Summary

Modifier and TypeField and Description
private final Set<ResourceLeakDetector.DefaultResourceLeak<?>>
private volatile int
private static final AtomicIntegerFieldUpdater<ResourceLeakDetector.DefaultResourceLeak<?>>
private volatile ResourceLeakDetector.TraceRecord
private static final AtomicReferenceFieldUpdater<ResourceLeakDetector.DefaultResourceLeak<?>, ResourceLeakDetector.TraceRecord>
private final int

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public boolean
close()

Implements io.netty.util.ResourceLeak.close.

Close the leak so that ResourceLeakDetector does not warn about leaked resources.
public boolean
close(T trackedObject)

Implements io.netty.util.ResourceLeakTracker.close.

Close the leak so that ResourceLeakTracker does not warn about leaked resources.
pack-priv boolean
private String
pack-priv String
private static void
reachabilityFence0(Object
the reference. If null, this method has no effect.
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.

public void
record()

Implements io.netty.util.ResourceLeakTracker.record, io.netty.util.ResourceLeak.record.

Records the caller's current stack trace so that the ResourceLeakDetector can tell where the leaked resource was accessed lastly.
public void
record(Object hint)

Implements io.netty.util.ResourceLeakTracker.record, io.netty.util.ResourceLeak.record.

Records the caller's current stack trace and the specified additional arbitrary information so that the ResourceLeakDetector can tell where the leaked resource was accessed lastly.
private void
record0(Object hint)

This method works by exponentially backing off as more records are present in the stack.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Field Detail

allLeaksback to summary
private final Set<ResourceLeakDetector.DefaultResourceLeak<?>> allLeaks
droppedRecordsback to summary
private volatile int droppedRecords
Annotations
@SuppressWarnings:unused
droppedRecordsUpdaterback to summary
private static final AtomicIntegerFieldUpdater<ResourceLeakDetector.DefaultResourceLeak<?>> droppedRecordsUpdater
Annotations
@SuppressWarnings:unchecked
headback to summary
private volatile ResourceLeakDetector.TraceRecord head
Annotations
@SuppressWarnings:unused
headUpdaterback to summary
private static final AtomicReferenceFieldUpdater<ResourceLeakDetector.DefaultResourceLeak<?>, ResourceLeakDetector.TraceRecord> headUpdater
Annotations
@SuppressWarnings:unchecked
trackedHashback to summary
private final int trackedHash

Constructor Detail

DefaultResourceLeakback to summary
pack-priv DefaultResourceLeak(Object referent, ReferenceQueue<Object> refQueue, Set<ResourceLeakDetector.DefaultResourceLeak<?>> allLeaks, Object initialHint)

Method Detail

closeback to summary
public boolean close()

Implements io.netty.util.ResourceLeak.close.

Doc from io.netty.util.ResourceLeak.close.

Close the leak so that ResourceLeakDetector does not warn about leaked resources.

Returns:boolean

true if called first time, false if called already

Annotations
@Override
closeback to summary
public boolean close(T trackedObject)

Implements io.netty.util.ResourceLeakTracker.close.

Doc from io.netty.util.ResourceLeakTracker.close.

Close the leak so that ResourceLeakTracker does not warn about leaked resources. After this method is called a leak associated with this ResourceLeakTracker should not be reported.

Returns:boolean

true if called first time, false if called already

Annotations
@Override
disposeback to summary
pack-priv boolean dispose()
generateReportback to summary
private String generateReport(ResourceLeakDetector.TraceRecord oldHead)
getReportAndClearRecordsback to summary
pack-priv String getReportAndClearRecords()
reachabilityFence0back to summary
private static void reachabilityFence0(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 as Reference.reachabilityFence for 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:Object

the reference. If null, this method has no effect.

See Also
java.lang.ref.Reference#reachabilityFence
recordback to summary
public void record()

Implements io.netty.util.ResourceLeakTracker.record, io.netty.util.ResourceLeak.record.

Doc from io.netty.util.ResourceLeakTracker.record.

Records the caller's current stack trace so that the ResourceLeakDetector can tell where the leaked resource was accessed lastly. This method is a shortcut to record(null).

Annotations
@Override
recordback to summary
public void record(Object hint)

Implements io.netty.util.ResourceLeakTracker.record, io.netty.util.ResourceLeak.record.

Doc from io.netty.util.ResourceLeakTracker.record.

Records the caller's current stack trace and the specified additional arbitrary information so that the ResourceLeakDetector can tell where the leaked resource was accessed lastly.

Annotations
@Override
record0back to summary
private void record0(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:

  1. 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.
  2. The very last access will always be recorded. This comes as a property of 1.
  3. It is possible to retain more records than the target, based upon the probability distribution.
  4. 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.
In this particular implementation, there are also some advantages. A thread local random is used to decide if something should be recorded. This means that if there is a deterministic access pattern, it is now possible to see what other accesses occur, rather than always dropping them. Second, after TARGET_RECORDS accesses, 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.
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object

Annotations
@Override
io.netty.util back to summary

public Interface ResourceLeakDetector.LeakListener


Method Summary

Modifier and TypeMethod and Description
public void
onLeak(String resourceType, String records)

Will be called once a leak is detected.

Method Detail

onLeakback to summary
public void onLeak(String resourceType, String records)

Will be called once a leak is detected.

io.netty.util back to summary

public final Enum ResourceLeakDetector.Level

extends Enum<ResourceLeakDetector.Level>
Class Inheritance

Represents the level of resource leak detection.

Field Summary

Modifier and TypeField and Description
public static final ResourceLeakDetector.Level
ADVANCED

Enables advanced sampling resource leak detection which reports where the leaked object was accessed recently at the cost of high overhead.

public static final ResourceLeakDetector.Level
DISABLED

Disables resource leak detection.

public static final ResourceLeakDetector.Level
PARANOID

Enables paranoid resource leak detection which reports where the leaked object was accessed recently, at the cost of the highest possible overhead (for testing purposes only).

public static final ResourceLeakDetector.Level
SIMPLE

Enables simplistic sampling resource leak detection which reports there is a leak or not, at the cost of small overhead (default).

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
pack-priv static ResourceLeakDetector.Level

Returns:

corresponding level or SIMPLE level in case of no match.
parseLevel
(String
- level string : DISABLED, SIMPLE, ADVANCED, PARANOID. Ignores case.
levelStr
)

Returns level based on string value.

public static ResourceLeakDetector.Level
public static ResourceLeakDetector.Level[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

ADVANCEDback to summary
public static final ResourceLeakDetector.Level ADVANCED

Enables advanced sampling resource leak detection which reports where the leaked object was accessed recently at the cost of high overhead.

DISABLEDback to summary
public static final ResourceLeakDetector.Level DISABLED

Disables resource leak detection.

PARANOIDback to summary
public static final ResourceLeakDetector.Level PARANOID

Enables paranoid resource leak detection which reports where the leaked object was accessed recently, at the cost of the highest possible overhead (for testing purposes only).

SIMPLEback to summary
public static final ResourceLeakDetector.Level SIMPLE

Enables simplistic sampling resource leak detection which reports there is a leak or not, at the cost of small overhead (default).

Constructor Detail

Levelback to summary
private Level()

Method Detail

parseLevelback to summary
pack-priv static ResourceLeakDetector.Level parseLevel(String levelStr)

Returns level based on string value. Accepts also string that represents ordinal number of enum.

Parameters
levelStr:String

- level string : DISABLED, SIMPLE, ADVANCED, PARANOID. Ignores case.

Returns:ResourceLeakDetector.Level

corresponding level or SIMPLE level in case of no match.

valueOfback to summary
public static ResourceLeakDetector.Level valueOf(String name)
valuesback to summary
public static ResourceLeakDetector.Level[] values()
io.netty.util back to summary

private Class ResourceLeakDetector.TraceRecord

extends Throwable
Class Inheritance

Field Summary

Modifier and TypeField and Description
private static final ResourceLeakDetector.TraceRecord
private final String
private final ResourceLeakDetector.TraceRecord
private final int
private static final long

Constructor Summary

AccessConstructor and Description
pack-priv
pack-priv
private

Method Summary

Modifier and TypeMethod and Description
public String
toString()

Overrides java.lang.Throwable.toString.

Returns a short description of this throwable.
Inherited from java.lang.Throwable:
addSuppressedfillInStackTracegetCausegetLocalizedMessagegetMessagegetStackTracegetSuppressedinitCauseprintStackTraceprintStackTraceprintStackTracesetStackTrace

Field Detail

BOTTOMback to summary
private static final ResourceLeakDetector.TraceRecord BOTTOM
hintStringback to summary
private final String hintString
nextback to summary
private final ResourceLeakDetector.TraceRecord next
posback to summary
private final int pos
serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.lang.Throwable.serialVersionUID.

Constructor Detail

TraceRecordback to summary
pack-priv TraceRecord(ResourceLeakDetector.TraceRecord next, Object hint)
TraceRecordback to summary
pack-priv TraceRecord(ResourceLeakDetector.TraceRecord next)
TraceRecordback to summary
private TraceRecord()

Method Detail

toStringback to summary
public String toString()

Overrides java.lang.Throwable.toString.

Doc from java.lang.Throwable.toString.

Returns a short description of this throwable. The result is the concatenation of:

  • the name of the class of this object
  • ": " (a colon and a space)
  • the result of invoking this object's getLocalizedMessage method
If getLocalizedMessage returns null, then just the class name is returned.
Returns:String

a string representation of this throwable.

Annotations
@Override