diff --git a/common/src/main/java/io/netty/util/ResourceLeakDetector.java b/common/src/main/java/io/netty/util/ResourceLeakDetector.java index aace44ed9596..67b7514ff0e0 100644 --- a/common/src/main/java/io/netty/util/ResourceLeakDetector.java +++ b/common/src/main/java/io/netty/util/ResourceLeakDetector.java @@ -27,6 +27,7 @@ import java.util.Deque; import java.util.concurrent.ConcurrentMap; +import static io.netty.util.internal.EmptyArrays.EMPTY_OBJECTS; 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; @@ -116,7 +117,6 @@ static Level parseLevel(String levelStr) { } } - // Should be power of two. static final int DEFAULT_SAMPLING_INTERVAL = 128; /** @@ -329,7 +329,7 @@ protected void reportInstancesLeak(String resourceType) { private final class DefaultResourceLeak extends PhantomReference implements ResourceLeakTracker, ResourceLeak { private final String creationRecord; - private final Deque lastRecords = new ArrayDeque(); + private final Deque lastRecords; private final int trackedHash; private int removedRecords; @@ -347,8 +347,10 @@ private final class DefaultResourceLeak extends PhantomReference impleme Level level = getLevel(); if (level.ordinal() >= Level.ADVANCED.ordinal()) { creationRecord = newRecord(null, 3); + lastRecords = new ArrayDeque(); } else { creationRecord = null; + lastRecords = null; } allLeaks.put(this, LeakEntry.INSTANCE); } @@ -407,9 +409,14 @@ public String toString() { final Object[] array; final int removedRecords; - synchronized (lastRecords) { - array = lastRecords.toArray(); - removedRecords = this.removedRecords; + if (lastRecords != null) { + synchronized (lastRecords) { + array = lastRecords.toArray(); + removedRecords = this.removedRecords; + } + } else { + removedRecords = 0; + array = EMPTY_OBJECTS; } StringBuilder buf = new StringBuilder(16384).append(NEWLINE);