Skip to content

Commit

Permalink
Change the contract of ResourceLeakDetector.open() so that unsampled …
Browse files Browse the repository at this point in the history
…resources are recycled

- This also fixes the problem introduced while trying to implement netty#1612 (Allow to disable resource leak detection).
  • Loading branch information
trustin committed Jul 23, 2013
1 parent d4aa5b5 commit 764741c
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 16 deletions.
4 changes: 3 additions & 1 deletion buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java
Original file line number Diff line number Diff line change
Expand Up @@ -1568,7 +1568,9 @@ protected void deallocate() {
c.freeIfNecessary();
}

leak.close();
if (leak != null) {
leak.close();
}
}

@Override
Expand Down
4 changes: 1 addition & 3 deletions buffer/src/main/java/io/netty/buffer/PooledByteBuf.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import io.netty.util.Recycler;
import io.netty.util.ResourceLeak;
import io.netty.util.ResourceLeakDetector;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
Expand Down Expand Up @@ -145,7 +144,7 @@ protected final void deallocate() {
this.handle = -1;
memory = null;
chunk.arena.free(chunk, handle);
if (ResourceLeakDetector.isEnabled()) {
if (leak != null) {
leak.close();
} else {
recycle();
Expand All @@ -157,7 +156,6 @@ protected final void deallocate() {
private void recycle() {
Recycler.Handle recyclerHandle = this.recyclerHandle;
if (recyclerHandle != null) {
setRefCnt(1);
((Recycler<Object>) recycler()).recycle(this, recyclerHandle);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ protected PooledDirectByteBuf newObject(Handle handle) {

static PooledDirectByteBuf newInstance(int maxCapacity) {
PooledDirectByteBuf buf = RECYCLER.get();
buf.setRefCnt(1);
buf.maxCapacity(maxCapacity);
return buf;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ protected PooledHeapByteBuf newObject(Handle handle) {

static PooledHeapByteBuf newInstance(int maxCapacity) {
PooledHeapByteBuf buf = RECYCLER.get();
buf.setRefCnt(1);
buf.maxCapacity(maxCapacity);
return buf;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ protected PooledUnsafeDirectByteBuf newObject(Handle handle) {

static PooledUnsafeDirectByteBuf newInstance(int maxCapacity) {
PooledUnsafeDirectByteBuf buf = RECYCLER.get();
buf.setRefCnt(1);
buf.maxCapacity(maxCapacity);
return buf;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ public ReadOnlyByteBufferBuf(ByteBufAllocator allocator, ByteBuffer buffer) {

@Override
protected void deallocate() {
leak.close();
if (leak != null) {
leak.close();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,10 @@ protected void deallocate() {
if (!doNotFree) {
PlatformDependent.freeDirectBuffer(buffer);
}
leak.close();

if (leak != null) {
leak.close();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,10 @@ protected void deallocate() {
if (!doNotFree) {
PlatformDependent.freeDirectBuffer(buffer);
}
leak.close();

if (leak != null) {
leak.close();
}
}

@Override
Expand Down
4 changes: 3 additions & 1 deletion common/src/main/java/io/netty/util/HashedWheelTimer.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,9 @@ public Set<Timeout> stop() {
Thread.currentThread().interrupt();
}

leak.close();
if (leak != null) {
leak.close();
}

Set<Timeout> unprocessedTimeouts = new HashSet<Timeout>();
for (Set<HashedWheelTimeout> bucket: wheel) {
Expand Down
15 changes: 7 additions & 8 deletions common/src/main/java/io/netty/util/ResourceLeakDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,6 @@ public final class ResourceLeakDetector<T> {

private static final int DEFAULT_SAMPLING_INTERVAL = 113;

private static final ResourceLeak NOOP = new ResourceLeak() {
@Override
public boolean close() {
return false;
}
};

/**
* Enables or disabled the resource leak detection.
*/
Expand Down Expand Up @@ -107,9 +100,15 @@ public ResourceLeakDetector(String resourceType, int samplingInterval, long maxA
tail.prev = head;
}

/**
* 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}
*/
public ResourceLeak open(T obj) {
if (disabled || leakCheckCnt ++ % samplingInterval != 0) {
return NOOP;
return null;
}

reportLeak();
Expand Down

0 comments on commit 764741c

Please sign in to comment.