Skip to content

Commit

Permalink
Remove allocation from ResourceLeakDetector
Browse files Browse the repository at this point in the history
Motivation:
RLD allocates an ArrayDeque in anticipation of recording access
points.  If the leak detection level is less than ADVANCED though,
the dequeue is never used.  Since SIMPLE is the default level,
there is a minor perf win to not preemptively allocate it.

This showed up in garbage profiling when creation a high number of
buffers.

Modifications:
Only allocate the dequeue if it will be used.

Result:
Less garbage created.
  • Loading branch information
carl-mastrangelo authored and normanmaurer committed Sep 16, 2017
1 parent b1332bf commit b32cd26
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions common/src/main/java/io/netty/util/ResourceLeakDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -116,7 +117,6 @@ static Level parseLevel(String levelStr) {
}
}

// Should be power of two.
static final int DEFAULT_SAMPLING_INTERVAL = 128;

/**
Expand Down Expand Up @@ -329,7 +329,7 @@ protected void reportInstancesLeak(String resourceType) {
private final class DefaultResourceLeak extends PhantomReference<Object> implements ResourceLeakTracker<T>,
ResourceLeak {
private final String creationRecord;
private final Deque<String> lastRecords = new ArrayDeque<String>();
private final Deque<String> lastRecords;
private final int trackedHash;

private int removedRecords;
Expand All @@ -347,8 +347,10 @@ private final class DefaultResourceLeak extends PhantomReference<Object> impleme
Level level = getLevel();
if (level.ordinal() >= Level.ADVANCED.ordinal()) {
creationRecord = newRecord(null, 3);
lastRecords = new ArrayDeque<String>();
} else {
creationRecord = null;
lastRecords = null;
}
allLeaks.put(this, LeakEntry.INSTANCE);
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b32cd26

Please sign in to comment.