2013-01-11 14:47:54 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2013 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package io.netty.util;
|
|
|
|
|
2013-02-27 00:54:51 +01:00
|
|
|
import io.netty.util.internal.PlatformDependent;
|
2013-01-11 14:47:54 +01:00
|
|
|
import io.netty.util.internal.SystemPropertyUtil;
|
2013-02-26 23:54:25 +01:00
|
|
|
import io.netty.util.internal.logging.InternalLogger;
|
|
|
|
import io.netty.util.internal.logging.InternalLoggerFactory;
|
2013-01-11 14:47:54 +01:00
|
|
|
|
2013-02-09 04:14:52 +01:00
|
|
|
import java.lang.ref.PhantomReference;
|
2013-01-11 14:47:54 +01:00
|
|
|
import java.lang.ref.ReferenceQueue;
|
2017-09-17 16:32:31 +02:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
2013-01-11 14:47:54 +01:00
|
|
|
import java.util.concurrent.ConcurrentMap;
|
|
|
|
|
2016-02-10 18:42:58 +01:00
|
|
|
import static io.netty.util.internal.StringUtil.EMPTY_STRING;
|
|
|
|
import static io.netty.util.internal.StringUtil.NEWLINE;
|
|
|
|
import static io.netty.util.internal.StringUtil.simpleClassName;
|
2013-12-04 11:03:32 +01:00
|
|
|
|
2016-06-15 09:19:15 +02:00
|
|
|
public class ResourceLeakDetector<T> {
|
2013-01-11 14:47:54 +01:00
|
|
|
|
2015-08-29 00:05:52 +02:00
|
|
|
private static final String PROP_LEVEL_OLD = "io.netty.leakDetectionLevel";
|
|
|
|
private static final String PROP_LEVEL = "io.netty.leakDetection.level";
|
2013-12-04 11:03:32 +01:00
|
|
|
private static final Level DEFAULT_LEVEL = Level.SIMPLE;
|
|
|
|
|
2015-08-29 00:05:52 +02:00
|
|
|
private static final String PROP_MAX_RECORDS = "io.netty.leakDetection.maxRecords";
|
|
|
|
private static final int DEFAULT_MAX_RECORDS = 4;
|
2017-09-17 16:32:31 +02:00
|
|
|
private static final String PROP_MAX_SAMPLED_RECORDS = "io.netty.leakDetection.maxSampledRecords";
|
|
|
|
|
2015-08-29 00:05:52 +02:00
|
|
|
private static final int MAX_RECORDS;
|
2017-09-17 16:32:31 +02:00
|
|
|
private static final int MAX_SAMPLED_RECORDS;
|
2015-08-29 00:05:52 +02:00
|
|
|
|
2013-12-04 11:03:32 +01:00
|
|
|
/**
|
|
|
|
* Represents the level of resource leak detection.
|
|
|
|
*/
|
|
|
|
public enum Level {
|
|
|
|
/**
|
|
|
|
* Disables resource leak detection.
|
|
|
|
*/
|
|
|
|
DISABLED,
|
|
|
|
/**
|
|
|
|
* Enables simplistic sampling resource leak detection which reports there is a leak or not,
|
|
|
|
* at the cost of small overhead (default).
|
|
|
|
*/
|
|
|
|
SIMPLE,
|
|
|
|
/**
|
|
|
|
* Enables advanced sampling resource leak detection which reports where the leaked object was accessed
|
|
|
|
* recently at the cost of high overhead.
|
|
|
|
*/
|
|
|
|
ADVANCED,
|
|
|
|
/**
|
|
|
|
* 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).
|
|
|
|
*/
|
2017-01-15 16:51:58 +01:00
|
|
|
PARANOID;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns level based on string value. Accepts also string that represents ordinal number of enum.
|
|
|
|
*
|
|
|
|
* @param levelStr - level string : DISABLED, SIMPLE, ADVANCED, PARANOID. Ignores case.
|
|
|
|
* @return corresponding level or SIMPLE level in case of no match.
|
|
|
|
*/
|
|
|
|
static Level parseLevel(String levelStr) {
|
|
|
|
String trimmedLevelStr = levelStr.trim();
|
|
|
|
for (Level l : values()) {
|
|
|
|
if (trimmedLevelStr.equalsIgnoreCase(l.name()) || trimmedLevelStr.equals(String.valueOf(l.ordinal()))) {
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return DEFAULT_LEVEL;
|
|
|
|
}
|
2013-12-04 11:03:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static Level level;
|
2013-06-10 12:13:06 +02:00
|
|
|
|
2013-01-11 14:47:54 +01:00
|
|
|
private static final InternalLogger logger = InternalLoggerFactory.getInstance(ResourceLeakDetector.class);
|
|
|
|
|
|
|
|
static {
|
2013-12-04 17:39:48 +01:00
|
|
|
final boolean disabled;
|
|
|
|
if (SystemPropertyUtil.get("io.netty.noResourceLeakDetection") != null) {
|
|
|
|
disabled = SystemPropertyUtil.getBoolean("io.netty.noResourceLeakDetection", false);
|
|
|
|
logger.debug("-Dio.netty.noResourceLeakDetection: {}", disabled);
|
|
|
|
logger.warn(
|
|
|
|
"-Dio.netty.noResourceLeakDetection is deprecated. Use '-D{}={}' instead.",
|
|
|
|
PROP_LEVEL, DEFAULT_LEVEL.name().toLowerCase());
|
|
|
|
} else {
|
|
|
|
disabled = false;
|
|
|
|
}
|
2013-12-04 17:02:38 +01:00
|
|
|
|
|
|
|
Level defaultLevel = disabled? Level.DISABLED : DEFAULT_LEVEL;
|
2015-08-29 00:05:52 +02:00
|
|
|
|
|
|
|
// First read old property name
|
2017-01-15 16:51:58 +01:00
|
|
|
String levelStr = SystemPropertyUtil.get(PROP_LEVEL_OLD, defaultLevel.name());
|
2015-08-29 00:05:52 +02:00
|
|
|
|
|
|
|
// If new property name is present, use it
|
2017-01-15 16:51:58 +01:00
|
|
|
levelStr = SystemPropertyUtil.get(PROP_LEVEL, levelStr);
|
|
|
|
Level level = Level.parseLevel(levelStr);
|
2013-12-04 11:03:32 +01:00
|
|
|
|
2015-08-29 00:05:52 +02:00
|
|
|
MAX_RECORDS = SystemPropertyUtil.getInt(PROP_MAX_RECORDS, DEFAULT_MAX_RECORDS);
|
2017-09-17 16:32:31 +02:00
|
|
|
long maxRecordsSampled = SystemPropertyUtil.getLong(PROP_MAX_SAMPLED_RECORDS, MAX_RECORDS * 10L);
|
|
|
|
MAX_SAMPLED_RECORDS = Math.max((int) Math.min(Integer.MAX_VALUE, maxRecordsSampled), MAX_RECORDS);
|
2015-08-29 00:05:52 +02:00
|
|
|
|
2013-12-04 11:03:32 +01:00
|
|
|
ResourceLeakDetector.level = level;
|
|
|
|
if (logger.isDebugEnabled()) {
|
|
|
|
logger.debug("-D{}: {}", PROP_LEVEL, level.name().toLowerCase());
|
2015-08-29 00:05:52 +02:00
|
|
|
logger.debug("-D{}: {}", PROP_MAX_RECORDS, MAX_RECORDS);
|
2017-09-17 16:32:31 +02:00
|
|
|
logger.debug("-D{}: {}", PROP_MAX_SAMPLED_RECORDS, MAX_SAMPLED_RECORDS);
|
2013-12-04 11:03:32 +01:00
|
|
|
}
|
2013-01-11 14:47:54 +01:00
|
|
|
}
|
2013-04-03 04:44:30 +02:00
|
|
|
|
2016-06-28 21:20:40 +02:00
|
|
|
static final int DEFAULT_SAMPLING_INTERVAL = 128;
|
2013-01-11 14:47:54 +01:00
|
|
|
|
2013-12-04 17:02:38 +01:00
|
|
|
/**
|
2013-12-04 17:39:48 +01:00
|
|
|
* @deprecated Use {@link #setLevel(Level)} instead.
|
2013-12-04 17:02:38 +01:00
|
|
|
*/
|
|
|
|
@Deprecated
|
|
|
|
public static void setEnabled(boolean enabled) {
|
|
|
|
setLevel(enabled? Level.SIMPLE : Level.DISABLED);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns {@code true} if resource leak detection is enabled.
|
|
|
|
*/
|
|
|
|
public static boolean isEnabled() {
|
|
|
|
return getLevel().ordinal() > Level.DISABLED.ordinal();
|
|
|
|
}
|
|
|
|
|
2013-07-22 20:55:49 +02:00
|
|
|
/**
|
2013-12-04 11:03:32 +01:00
|
|
|
* Sets the resource leak detection level.
|
2013-07-22 20:55:49 +02:00
|
|
|
*/
|
2013-12-04 11:03:32 +01:00
|
|
|
public static void setLevel(Level level) {
|
|
|
|
if (level == null) {
|
|
|
|
throw new NullPointerException("level");
|
|
|
|
}
|
|
|
|
ResourceLeakDetector.level = level;
|
2013-07-22 20:55:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-12-04 11:03:32 +01:00
|
|
|
* Returns the current resource leak detection level.
|
2013-07-22 20:55:49 +02:00
|
|
|
*/
|
2013-12-04 11:03:32 +01:00
|
|
|
public static Level getLevel() {
|
|
|
|
return level;
|
2013-07-22 20:55:49 +02:00
|
|
|
}
|
|
|
|
|
2016-10-22 01:58:32 +02:00
|
|
|
/** the collection of active resources */
|
2016-12-01 08:13:33 +01:00
|
|
|
private final ConcurrentMap<DefaultResourceLeak, LeakEntry> allLeaks = PlatformDependent.newConcurrentHashMap();
|
2013-01-11 14:47:54 +01:00
|
|
|
|
|
|
|
private final ReferenceQueue<Object> refQueue = new ReferenceQueue<Object>();
|
2013-12-04 11:03:32 +01:00
|
|
|
private final ConcurrentMap<String, Boolean> reportedLeaks = PlatformDependent.newConcurrentHashMap();
|
2013-01-11 14:47:54 +01:00
|
|
|
|
|
|
|
private final String resourceType;
|
|
|
|
private final int samplingInterval;
|
|
|
|
|
2016-06-28 21:20:40 +02:00
|
|
|
/**
|
|
|
|
* @deprecated use {@link ResourceLeakDetectorFactory#newResourceLeakDetector(Class, int, long)}.
|
|
|
|
*/
|
|
|
|
@Deprecated
|
2013-01-11 14:47:54 +01:00
|
|
|
public ResourceLeakDetector(Class<?> resourceType) {
|
2013-12-04 11:03:32 +01:00
|
|
|
this(simpleClassName(resourceType));
|
2013-01-11 14:47:54 +01:00
|
|
|
}
|
|
|
|
|
2016-06-28 21:20:40 +02:00
|
|
|
/**
|
|
|
|
* @deprecated use {@link ResourceLeakDetectorFactory#newResourceLeakDetector(Class, int, long)}.
|
|
|
|
*/
|
|
|
|
@Deprecated
|
2013-01-11 14:47:54 +01:00
|
|
|
public ResourceLeakDetector(String resourceType) {
|
|
|
|
this(resourceType, DEFAULT_SAMPLING_INTERVAL, Long.MAX_VALUE);
|
|
|
|
}
|
|
|
|
|
2016-06-28 21:20:40 +02:00
|
|
|
/**
|
2017-01-18 19:56:25 +01:00
|
|
|
* @deprecated Use {@link ResourceLeakDetector#ResourceLeakDetector(Class, int)}.
|
|
|
|
* <p>
|
2016-06-28 21:20:40 +02:00
|
|
|
* This should not be used directly by users of {@link ResourceLeakDetector}.
|
|
|
|
* Please use {@link ResourceLeakDetectorFactory#newResourceLeakDetector(Class)}
|
|
|
|
* or {@link ResourceLeakDetectorFactory#newResourceLeakDetector(Class, int, long)}
|
2017-01-18 19:56:25 +01:00
|
|
|
*
|
|
|
|
* @param maxActive This is deprecated and will be ignored.
|
2016-06-28 21:20:40 +02:00
|
|
|
*/
|
2017-01-18 19:56:25 +01:00
|
|
|
@Deprecated
|
2013-01-11 14:47:54 +01:00
|
|
|
public ResourceLeakDetector(Class<?> resourceType, int samplingInterval, long maxActive) {
|
2017-01-18 19:56:25 +01:00
|
|
|
this(resourceType, samplingInterval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This should not be used directly by users of {@link ResourceLeakDetector}.
|
|
|
|
* Please use {@link ResourceLeakDetectorFactory#newResourceLeakDetector(Class)}
|
|
|
|
* or {@link ResourceLeakDetectorFactory#newResourceLeakDetector(Class, int, long)}
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
public ResourceLeakDetector(Class<?> resourceType, int samplingInterval) {
|
|
|
|
this(simpleClassName(resourceType), samplingInterval, Long.MAX_VALUE);
|
2013-01-11 14:47:54 +01:00
|
|
|
}
|
|
|
|
|
2016-06-28 21:20:40 +02:00
|
|
|
/**
|
|
|
|
* @deprecated use {@link ResourceLeakDetectorFactory#newResourceLeakDetector(Class, int, long)}.
|
2017-01-18 19:56:25 +01:00
|
|
|
* <p>
|
|
|
|
* @param maxActive This is deprecated and will be ignored.
|
2016-06-28 21:20:40 +02:00
|
|
|
*/
|
|
|
|
@Deprecated
|
2013-01-11 14:47:54 +01:00
|
|
|
public ResourceLeakDetector(String resourceType, int samplingInterval, long maxActive) {
|
|
|
|
if (resourceType == null) {
|
|
|
|
throw new NullPointerException("resourceType");
|
|
|
|
}
|
|
|
|
|
|
|
|
this.resourceType = resourceType;
|
2016-10-22 01:58:32 +02:00
|
|
|
this.samplingInterval = samplingInterval;
|
2013-01-11 14:47:54 +01:00
|
|
|
}
|
|
|
|
|
2013-07-23 07:06:58 +02:00
|
|
|
/**
|
|
|
|
* Creates a new {@link ResourceLeak} which is expected to be closed via {@link ResourceLeak#close()} when the
|
|
|
|
* related resource is deallocated.
|
|
|
|
*
|
|
|
|
* @return the {@link ResourceLeak} or {@code null}
|
2016-12-01 08:36:16 +01:00
|
|
|
* @deprecated use {@link #track(Object)}
|
2013-07-23 07:06:58 +02:00
|
|
|
*/
|
2016-12-01 08:36:16 +01:00
|
|
|
@Deprecated
|
2016-06-28 21:20:40 +02:00
|
|
|
public final ResourceLeak open(T obj) {
|
2016-12-01 08:36:16 +01:00
|
|
|
return track0(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new {@link ResourceLeakTracker} which is expected to be closed via
|
|
|
|
* {@link ResourceLeakTracker#close(Object)} when the related resource is deallocated.
|
|
|
|
*
|
|
|
|
* @return the {@link ResourceLeakTracker} or {@code null}
|
|
|
|
*/
|
|
|
|
public final ResourceLeakTracker<T> track(T obj) {
|
|
|
|
return track0(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
private DefaultResourceLeak track0(T obj) {
|
2013-12-04 11:03:32 +01:00
|
|
|
Level level = ResourceLeakDetector.level;
|
|
|
|
if (level == Level.DISABLED) {
|
2013-07-23 07:06:58 +02:00
|
|
|
return null;
|
2013-01-11 14:47:54 +01:00
|
|
|
}
|
|
|
|
|
2013-12-04 11:03:32 +01:00
|
|
|
if (level.ordinal() < Level.PARANOID.ordinal()) {
|
2017-02-15 08:12:40 +01:00
|
|
|
if ((PlatformDependent.threadLocalRandom().nextInt(samplingInterval)) == 0) {
|
2017-09-17 16:32:31 +02:00
|
|
|
reportLeak();
|
2013-12-04 11:03:32 +01:00
|
|
|
return new DefaultResourceLeak(obj);
|
|
|
|
}
|
2017-09-17 16:32:31 +02:00
|
|
|
return null;
|
2013-12-04 11:03:32 +01:00
|
|
|
}
|
2017-09-17 16:32:31 +02:00
|
|
|
reportLeak();
|
|
|
|
return new DefaultResourceLeak(obj);
|
2013-01-11 14:47:54 +01:00
|
|
|
}
|
|
|
|
|
2017-09-17 16:32:31 +02:00
|
|
|
private void clearRefQueue() {
|
|
|
|
for (;;) {
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
DefaultResourceLeak ref = (DefaultResourceLeak) refQueue.poll();
|
|
|
|
if (ref == null) {
|
|
|
|
break;
|
2013-01-11 14:47:54 +01:00
|
|
|
}
|
2017-09-21 17:56:49 +02:00
|
|
|
ref.dispose();
|
2017-09-17 16:32:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void reportLeak() {
|
|
|
|
if (!logger.isErrorEnabled()) {
|
|
|
|
clearRefQueue();
|
2013-01-11 14:47:54 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect and report previous leaks.
|
|
|
|
for (;;) {
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
DefaultResourceLeak ref = (DefaultResourceLeak) refQueue.poll();
|
|
|
|
if (ref == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-09-21 17:56:49 +02:00
|
|
|
if (!ref.dispose()) {
|
2013-01-11 14:47:54 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-12-04 11:03:32 +01:00
|
|
|
String records = ref.toString();
|
|
|
|
if (reportedLeaks.putIfAbsent(records, Boolean.TRUE) == null) {
|
|
|
|
if (records.isEmpty()) {
|
2016-06-15 09:19:15 +02:00
|
|
|
reportUntracedLeak(resourceType);
|
2013-12-04 11:03:32 +01:00
|
|
|
} else {
|
2016-06-15 09:19:15 +02:00
|
|
|
reportTracedLeak(resourceType, records);
|
2013-12-04 11:03:32 +01:00
|
|
|
}
|
2013-01-11 14:47:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-15 09:19:15 +02:00
|
|
|
/**
|
|
|
|
* This method is called when a traced leak is detected. It can be overridden for tracking how many times leaks
|
|
|
|
* have been detected.
|
|
|
|
*/
|
|
|
|
protected void reportTracedLeak(String resourceType, String records) {
|
|
|
|
logger.error(
|
|
|
|
"LEAK: {}.release() was not called before it's garbage-collected. " +
|
|
|
|
"See http://netty.io/wiki/reference-counted-objects.html for more information.{}",
|
|
|
|
resourceType, records);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method is called when an untraced leak is detected. It can be overridden for tracking how many times leaks
|
|
|
|
* have been detected.
|
|
|
|
*/
|
|
|
|
protected void reportUntracedLeak(String resourceType) {
|
|
|
|
logger.error("LEAK: {}.release() was not called before it's garbage-collected. " +
|
|
|
|
"Enable advanced leak reporting to find out where the leak occurred. " +
|
|
|
|
"To enable advanced leak reporting, " +
|
|
|
|
"specify the JVM option '-D{}={}' or call {}.setLevel() " +
|
|
|
|
"See http://netty.io/wiki/reference-counted-objects.html for more information.",
|
|
|
|
resourceType, PROP_LEVEL, Level.ADVANCED.name().toLowerCase(), simpleClassName(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-18 19:56:25 +01:00
|
|
|
* @deprecated This method will no longer be invoked by {@link ResourceLeakDetector}.
|
2016-06-15 09:19:15 +02:00
|
|
|
*/
|
2017-01-18 19:56:25 +01:00
|
|
|
@Deprecated
|
2016-06-15 09:19:15 +02:00
|
|
|
protected void reportInstancesLeak(String resourceType) {
|
|
|
|
}
|
|
|
|
|
2016-12-01 08:36:16 +01:00
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
private final class DefaultResourceLeak extends PhantomReference<Object> implements ResourceLeakTracker<T>,
|
|
|
|
ResourceLeak {
|
2017-09-17 16:32:31 +02:00
|
|
|
private final Record head;
|
2016-12-01 08:36:16 +01:00
|
|
|
|
2017-09-17 16:32:31 +02:00
|
|
|
// This will be updated once a new Record will be created and added
|
|
|
|
private Record tail;
|
|
|
|
|
|
|
|
private final int trackedHash;
|
|
|
|
private int numRecords;
|
|
|
|
private int droppedRecords;
|
2013-01-11 14:47:54 +01:00
|
|
|
|
2014-01-29 03:52:28 +01:00
|
|
|
DefaultResourceLeak(Object referent) {
|
2016-12-01 08:13:33 +01:00
|
|
|
super(referent, refQueue);
|
2013-01-11 14:47:54 +01:00
|
|
|
|
2016-12-01 08:13:33 +01:00
|
|
|
assert referent != null;
|
2013-01-11 14:47:54 +01:00
|
|
|
|
2016-12-01 08:36:16 +01:00
|
|
|
// Store the hash of the tracked object to later assert it in the close(...) method.
|
|
|
|
// It's important that we not store a reference to the referent as this would disallow it from
|
|
|
|
// be collected via the PhantomReference.
|
|
|
|
trackedHash = System.identityHashCode(referent);
|
2017-09-17 16:32:31 +02:00
|
|
|
head = tail = getLevel().ordinal() >= Level.ADVANCED.ordinal() ? new Record() : null;
|
2016-12-01 08:13:33 +01:00
|
|
|
allLeaks.put(this, LeakEntry.INSTANCE);
|
2013-01-11 14:47:54 +01:00
|
|
|
}
|
|
|
|
|
2013-12-04 11:03:32 +01:00
|
|
|
@Override
|
|
|
|
public void record() {
|
2017-09-17 16:32:31 +02:00
|
|
|
record0(null);
|
2014-01-29 03:44:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void record(Object hint) {
|
2017-09-17 16:32:31 +02:00
|
|
|
record0(hint);
|
2014-01-29 03:44:59 +01:00
|
|
|
}
|
|
|
|
|
2017-09-17 16:32:31 +02:00
|
|
|
private void record0(Object hint) {
|
2017-07-17 05:04:35 +02:00
|
|
|
// Check MAX_RECORDS > 0 here to avoid similar check before remove from and add to lastRecords
|
2017-09-17 16:32:31 +02:00
|
|
|
if (head != null && MAX_RECORDS > 0) {
|
|
|
|
synchronized (head) {
|
|
|
|
if (tail == null) {
|
|
|
|
// already closed
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Record record = hint == null ? new Record() : new Record(hint);
|
|
|
|
tail.next = record;
|
|
|
|
tail = record;
|
|
|
|
|
|
|
|
// Enforce a limit so our linked-list not grows too large and cause a GC storm later on when we
|
|
|
|
// unlink it. The reason why we choose a different limit to MAX_RECORDS is that we will not handle
|
|
|
|
// duplications here as its very expensive. We will filter these out when we actually
|
|
|
|
// detected a leak.
|
|
|
|
if (numRecords == MAX_SAMPLED_RECORDS) {
|
|
|
|
head.next = head.next.next;
|
|
|
|
droppedRecords++;
|
|
|
|
} else {
|
|
|
|
numRecords++;
|
2013-12-04 11:03:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-21 17:56:49 +02:00
|
|
|
boolean dispose() {
|
|
|
|
clear();
|
|
|
|
return allLeaks.remove(this, LeakEntry.INSTANCE);
|
|
|
|
}
|
|
|
|
|
2013-01-11 14:47:54 +01:00
|
|
|
@Override
|
|
|
|
public boolean close() {
|
2016-10-22 01:58:32 +02:00
|
|
|
// Use the ConcurrentMap remove method, which avoids allocating an iterator.
|
2017-09-17 16:32:31 +02:00
|
|
|
if (allLeaks.remove(this, LeakEntry.INSTANCE)) {
|
|
|
|
// Call clear so the reference is not even enqueued.
|
|
|
|
clear();
|
|
|
|
|
|
|
|
if (head != null) {
|
|
|
|
synchronized (head) {
|
|
|
|
// Allow to GC all the records.
|
|
|
|
head.next = null;
|
|
|
|
numRecords = 0;
|
|
|
|
tail = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2013-01-11 14:47:54 +01:00
|
|
|
}
|
2013-12-04 11:03:32 +01:00
|
|
|
|
2016-12-01 08:36:16 +01:00
|
|
|
@Override
|
|
|
|
public boolean close(T trackedObject) {
|
|
|
|
// Ensure that the object that was tracked is the same as the one that was passed to close(...).
|
|
|
|
assert trackedHash == System.identityHashCode(trackedObject);
|
|
|
|
|
|
|
|
// We need to actually do the null check of the trackedObject after we close the leak because otherwise
|
|
|
|
// we may get false-positives reported by the ResourceLeakDetector. This can happen as the JIT / GC may
|
|
|
|
// be able to figure out that we do not need the trackedObject anymore and so already enqueue it for
|
|
|
|
// collection before we actually get a chance to close the enclosing ResourceLeak.
|
|
|
|
return close() && trackedObject != null;
|
|
|
|
}
|
|
|
|
|
2015-01-08 04:43:03 +01:00
|
|
|
@Override
|
2013-12-04 11:03:32 +01:00
|
|
|
public String toString() {
|
2017-09-17 16:32:31 +02:00
|
|
|
if (head == null) {
|
2016-02-10 18:42:58 +01:00
|
|
|
return EMPTY_STRING;
|
2013-12-04 11:03:32 +01:00
|
|
|
}
|
|
|
|
|
2017-09-17 16:32:31 +02:00
|
|
|
final String creationRecord;
|
|
|
|
final String[] array;
|
|
|
|
int idx = 0;
|
|
|
|
String last = null;
|
|
|
|
final int dropped;
|
|
|
|
|
|
|
|
synchronized (head) {
|
|
|
|
if (tail == null) {
|
|
|
|
// Already closed
|
|
|
|
return EMPTY_STRING;
|
|
|
|
}
|
|
|
|
dropped = droppedRecords;
|
|
|
|
creationRecord = head.toString();
|
|
|
|
array = new String[numRecords];
|
|
|
|
Record record = head.next;
|
|
|
|
while (record != null) {
|
|
|
|
String recordStr = record.toString();
|
|
|
|
if (last == null || !last.equals(recordStr)) {
|
|
|
|
array[idx ++] = recordStr;
|
|
|
|
last = recordStr;
|
|
|
|
}
|
|
|
|
record = record.next;
|
2017-09-15 04:08:23 +02:00
|
|
|
}
|
2013-12-06 14:35:14 +01:00
|
|
|
}
|
2013-12-04 11:03:32 +01:00
|
|
|
|
2017-09-17 16:32:31 +02:00
|
|
|
int removed = idx > MAX_RECORDS ? idx - MAX_RECORDS : 0;
|
|
|
|
|
2016-02-10 18:42:58 +01:00
|
|
|
StringBuilder buf = new StringBuilder(16384).append(NEWLINE);
|
2017-09-17 16:32:31 +02:00
|
|
|
if (removed > 0) {
|
2016-02-10 18:42:58 +01:00
|
|
|
buf.append("WARNING: ")
|
2017-09-17 16:32:31 +02:00
|
|
|
.append(removed)
|
|
|
|
.append(" leak records were discarded because the leak record count is limited to ")
|
|
|
|
.append(MAX_RECORDS)
|
|
|
|
.append(". Use system property ")
|
|
|
|
.append(PROP_MAX_RECORDS)
|
|
|
|
.append(" to increase the limit.")
|
|
|
|
.append(NEWLINE);
|
|
|
|
}
|
|
|
|
if (dropped > 0) {
|
|
|
|
buf.append(dropped)
|
|
|
|
.append(" leak records were not sampled because the leak record sample count is limited to ")
|
|
|
|
.append(MAX_SAMPLED_RECORDS)
|
|
|
|
.append(". Use system property ")
|
|
|
|
.append(PROP_MAX_SAMPLED_RECORDS)
|
|
|
|
.append(" to increase the limit.")
|
|
|
|
.append(NEWLINE);
|
2016-02-10 18:42:58 +01:00
|
|
|
}
|
2013-12-04 11:03:32 +01:00
|
|
|
|
2017-09-17 16:32:31 +02:00
|
|
|
int records = idx - removed;
|
|
|
|
buf.append("Recent access records: ").append(records).append(NEWLINE);
|
|
|
|
|
|
|
|
if (records > 0) {
|
|
|
|
// The array may not be completely filled so we need to take this into account.
|
|
|
|
for (int i = records - 1; i >= 0; i --) {
|
2014-11-08 23:46:30 +01:00
|
|
|
buf.append('#')
|
|
|
|
.append(i + 1)
|
|
|
|
.append(':')
|
|
|
|
.append(NEWLINE)
|
|
|
|
.append(array[i]);
|
2013-12-04 11:03:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-08 23:46:30 +01:00
|
|
|
buf.append("Created at:")
|
|
|
|
.append(NEWLINE)
|
|
|
|
.append(creationRecord);
|
2013-12-04 11:03:32 +01:00
|
|
|
|
2014-11-08 23:46:30 +01:00
|
|
|
buf.setLength(buf.length() - NEWLINE.length());
|
2013-12-04 11:03:32 +01:00
|
|
|
return buf.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 16:32:31 +02:00
|
|
|
private static final class Record extends Throwable {
|
|
|
|
|
|
|
|
private static final Set<String> STACK_TRACE_ELEMENT_EXCLUSIONS = new HashSet<String>();
|
|
|
|
static {
|
|
|
|
STACK_TRACE_ELEMENT_EXCLUSIONS.add("io.netty.util.ReferenceCountUtil.touch");
|
|
|
|
STACK_TRACE_ELEMENT_EXCLUSIONS.add("io.netty.buffer.AdvancedLeakAwareByteBuf.touch");
|
|
|
|
STACK_TRACE_ELEMENT_EXCLUSIONS.add("io.netty.buffer.AbstractByteBufAllocator.toLeakAwareBuffer");
|
|
|
|
STACK_TRACE_ELEMENT_EXCLUSIONS.add(
|
|
|
|
"io.netty.buffer.AdvancedLeakAwareByteBuf.recordLeakNonRefCountingOperation");
|
2014-01-29 03:44:59 +01:00
|
|
|
}
|
|
|
|
|
2017-09-17 16:32:31 +02:00
|
|
|
private final String hintString;
|
|
|
|
Record next;
|
2014-01-29 03:52:28 +01:00
|
|
|
|
2017-09-17 16:32:31 +02:00
|
|
|
Record(Object hint) {
|
|
|
|
// This needs to be generated even if toString() is never called as it may change later on.
|
|
|
|
hintString = hint instanceof ResourceLeakHint ? ((ResourceLeakHint) hint).toHintString() : hint.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
Record() {
|
|
|
|
hintString = null;
|
|
|
|
}
|
2014-01-29 03:52:28 +01:00
|
|
|
|
2017-09-17 16:32:31 +02:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
StringBuilder buf = new StringBuilder(4096);
|
|
|
|
if (hintString != null) {
|
|
|
|
buf.append("\tHint: ").append(hintString).append(NEWLINE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append the stack trace.
|
|
|
|
StackTraceElement[] array = getStackTrace();
|
|
|
|
// Skip the first three elements.
|
|
|
|
for (int i = 3; i < array.length; i++) {
|
|
|
|
StackTraceElement element = array[i];
|
|
|
|
|
|
|
|
// Strip the noisy stack trace elements.
|
|
|
|
if (STACK_TRACE_ELEMENT_EXCLUSIONS.contains(element.getClassName() + '.' + element.getMethodName())) {
|
|
|
|
continue;
|
2014-01-29 03:52:28 +01:00
|
|
|
}
|
2017-09-17 16:32:31 +02:00
|
|
|
buf.append('\t');
|
|
|
|
buf.append(element.toString());
|
|
|
|
buf.append(NEWLINE);
|
2013-12-04 11:03:32 +01:00
|
|
|
}
|
2017-09-17 16:32:31 +02:00
|
|
|
return buf.toString();
|
2013-12-04 11:03:32 +01:00
|
|
|
}
|
2013-01-11 14:47:54 +01:00
|
|
|
}
|
2016-12-01 08:13:33 +01:00
|
|
|
|
|
|
|
private static final class LeakEntry {
|
|
|
|
static final LeakEntry INSTANCE = new LeakEntry();
|
|
|
|
private static final int HASH = System.identityHashCode(INSTANCE);
|
|
|
|
|
2016-12-01 08:36:16 +01:00
|
|
|
private LeakEntry() {
|
|
|
|
}
|
2016-12-01 08:13:33 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return HASH;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
return obj == this;
|
|
|
|
}
|
|
|
|
}
|
2013-01-11 14:47:54 +01:00
|
|
|
}
|