Additional configuration for leak detection

Motivation:

Leak detector, when it detects a leak, will print the last 5 stack
traces that touched the ByteBuf. In some cases that might not be enough
to identify the root cause of the leak.
Also, sometimes users might not be interested in tracing all the
operations on the buffer, but just the ones that are affecting the
reference count.

Modifications:

Added command line properties to override default values:
 * Allow to configure max number of stack traces to collect
 * Allow to only record retain/release operation on buffers

Result:
Users can increase the number of stack traces to debug buffer leaks
with lot of retain/release operations.
This commit is contained in:
Matteo Merli 2015-08-28 15:05:52 -07:00 committed by Norman Maurer
parent 09f8e428c3
commit 2d4a8a75bb
2 changed files with 138 additions and 113 deletions

View File

@ -17,6 +17,7 @@
package io.netty.buffer; package io.netty.buffer;
import io.netty.util.ResourceLeak; import io.netty.util.ResourceLeak;
import io.netty.util.internal.SystemPropertyUtil;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -29,6 +30,13 @@ import java.nio.charset.Charset;
final class AdvancedLeakAwareByteBuf extends WrappedByteBuf { final class AdvancedLeakAwareByteBuf extends WrappedByteBuf {
private static final String PROP_ACQUIRE_AND_RELEASE_ONLY = "io.netty.leakDetection.acquireAndReleaseOnly";
private static final boolean ACQUIRE_AND_RELEASE_ONLY;
static {
ACQUIRE_AND_RELEASE_ONLY = SystemPropertyUtil.getBoolean(PROP_ACQUIRE_AND_RELEASE_ONLY, false);
}
private final ResourceLeak leak; private final ResourceLeak leak;
AdvancedLeakAwareByteBuf(ByteBuf buf, ResourceLeak leak) { AdvancedLeakAwareByteBuf(ByteBuf buf, ResourceLeak leak) {
@ -58,9 +66,15 @@ final class AdvancedLeakAwareByteBuf extends WrappedByteBuf {
return deallocated; return deallocated;
} }
private void recordLeakNonRefCountingOperation() {
if (!ACQUIRE_AND_RELEASE_ONLY) {
leak.record();
}
}
@Override @Override
public ByteBuf order(ByteOrder endianness) { public ByteBuf order(ByteOrder endianness) {
leak.record(); recordLeakNonRefCountingOperation();
if (order() == endianness) { if (order() == endianness) {
return this; return this;
} else { } else {
@ -70,637 +84,637 @@ final class AdvancedLeakAwareByteBuf extends WrappedByteBuf {
@Override @Override
public ByteBuf slice() { public ByteBuf slice() {
leak.record(); recordLeakNonRefCountingOperation();
return new AdvancedLeakAwareByteBuf(super.slice(), leak); return new AdvancedLeakAwareByteBuf(super.slice(), leak);
} }
@Override @Override
public ByteBuf slice(int index, int length) { public ByteBuf slice(int index, int length) {
leak.record(); recordLeakNonRefCountingOperation();
return new AdvancedLeakAwareByteBuf(super.slice(index, length), leak); return new AdvancedLeakAwareByteBuf(super.slice(index, length), leak);
} }
@Override @Override
public ByteBuf duplicate() { public ByteBuf duplicate() {
leak.record(); recordLeakNonRefCountingOperation();
return new AdvancedLeakAwareByteBuf(super.duplicate(), leak); return new AdvancedLeakAwareByteBuf(super.duplicate(), leak);
} }
@Override @Override
public ByteBuf readSlice(int length) { public ByteBuf readSlice(int length) {
leak.record(); recordLeakNonRefCountingOperation();
return new AdvancedLeakAwareByteBuf(super.readSlice(length), leak); return new AdvancedLeakAwareByteBuf(super.readSlice(length), leak);
} }
@Override @Override
public ByteBuf discardReadBytes() { public ByteBuf discardReadBytes() {
leak.record(); recordLeakNonRefCountingOperation();
return super.discardReadBytes(); return super.discardReadBytes();
} }
@Override @Override
public ByteBuf discardSomeReadBytes() { public ByteBuf discardSomeReadBytes() {
leak.record(); recordLeakNonRefCountingOperation();
return super.discardSomeReadBytes(); return super.discardSomeReadBytes();
} }
@Override @Override
public ByteBuf ensureWritable(int minWritableBytes) { public ByteBuf ensureWritable(int minWritableBytes) {
leak.record(); recordLeakNonRefCountingOperation();
return super.ensureWritable(minWritableBytes); return super.ensureWritable(minWritableBytes);
} }
@Override @Override
public int ensureWritable(int minWritableBytes, boolean force) { public int ensureWritable(int minWritableBytes, boolean force) {
leak.record(); recordLeakNonRefCountingOperation();
return super.ensureWritable(minWritableBytes, force); return super.ensureWritable(minWritableBytes, force);
} }
@Override @Override
public boolean getBoolean(int index) { public boolean getBoolean(int index) {
leak.record(); recordLeakNonRefCountingOperation();
return super.getBoolean(index); return super.getBoolean(index);
} }
@Override @Override
public byte getByte(int index) { public byte getByte(int index) {
leak.record(); recordLeakNonRefCountingOperation();
return super.getByte(index); return super.getByte(index);
} }
@Override @Override
public short getUnsignedByte(int index) { public short getUnsignedByte(int index) {
leak.record(); recordLeakNonRefCountingOperation();
return super.getUnsignedByte(index); return super.getUnsignedByte(index);
} }
@Override @Override
public short getShort(int index) { public short getShort(int index) {
leak.record(); recordLeakNonRefCountingOperation();
return super.getShort(index); return super.getShort(index);
} }
@Override @Override
public int getUnsignedShort(int index) { public int getUnsignedShort(int index) {
leak.record(); recordLeakNonRefCountingOperation();
return super.getUnsignedShort(index); return super.getUnsignedShort(index);
} }
@Override @Override
public int getMedium(int index) { public int getMedium(int index) {
leak.record(); recordLeakNonRefCountingOperation();
return super.getMedium(index); return super.getMedium(index);
} }
@Override @Override
public int getUnsignedMedium(int index) { public int getUnsignedMedium(int index) {
leak.record(); recordLeakNonRefCountingOperation();
return super.getUnsignedMedium(index); return super.getUnsignedMedium(index);
} }
@Override @Override
public int getInt(int index) { public int getInt(int index) {
leak.record(); recordLeakNonRefCountingOperation();
return super.getInt(index); return super.getInt(index);
} }
@Override @Override
public long getUnsignedInt(int index) { public long getUnsignedInt(int index) {
leak.record(); recordLeakNonRefCountingOperation();
return super.getUnsignedInt(index); return super.getUnsignedInt(index);
} }
@Override @Override
public long getLong(int index) { public long getLong(int index) {
leak.record(); recordLeakNonRefCountingOperation();
return super.getLong(index); return super.getLong(index);
} }
@Override @Override
public char getChar(int index) { public char getChar(int index) {
leak.record(); recordLeakNonRefCountingOperation();
return super.getChar(index); return super.getChar(index);
} }
@Override @Override
public float getFloat(int index) { public float getFloat(int index) {
leak.record(); recordLeakNonRefCountingOperation();
return super.getFloat(index); return super.getFloat(index);
} }
@Override @Override
public double getDouble(int index) { public double getDouble(int index) {
leak.record(); recordLeakNonRefCountingOperation();
return super.getDouble(index); return super.getDouble(index);
} }
@Override @Override
public ByteBuf getBytes(int index, ByteBuf dst) { public ByteBuf getBytes(int index, ByteBuf dst) {
leak.record(); recordLeakNonRefCountingOperation();
return super.getBytes(index, dst); return super.getBytes(index, dst);
} }
@Override @Override
public ByteBuf getBytes(int index, ByteBuf dst, int length) { public ByteBuf getBytes(int index, ByteBuf dst, int length) {
leak.record(); recordLeakNonRefCountingOperation();
return super.getBytes(index, dst, length); return super.getBytes(index, dst, length);
} }
@Override @Override
public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
leak.record(); recordLeakNonRefCountingOperation();
return super.getBytes(index, dst, dstIndex, length); return super.getBytes(index, dst, dstIndex, length);
} }
@Override @Override
public ByteBuf getBytes(int index, byte[] dst) { public ByteBuf getBytes(int index, byte[] dst) {
leak.record(); recordLeakNonRefCountingOperation();
return super.getBytes(index, dst); return super.getBytes(index, dst);
} }
@Override @Override
public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) { public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
leak.record(); recordLeakNonRefCountingOperation();
return super.getBytes(index, dst, dstIndex, length); return super.getBytes(index, dst, dstIndex, length);
} }
@Override @Override
public ByteBuf getBytes(int index, ByteBuffer dst) { public ByteBuf getBytes(int index, ByteBuffer dst) {
leak.record(); recordLeakNonRefCountingOperation();
return super.getBytes(index, dst); return super.getBytes(index, dst);
} }
@Override @Override
public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException { public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException {
leak.record(); recordLeakNonRefCountingOperation();
return super.getBytes(index, out, length); return super.getBytes(index, out, length);
} }
@Override @Override
public int getBytes(int index, GatheringByteChannel out, int length) throws IOException { public int getBytes(int index, GatheringByteChannel out, int length) throws IOException {
leak.record(); recordLeakNonRefCountingOperation();
return super.getBytes(index, out, length); return super.getBytes(index, out, length);
} }
@Override @Override
public ByteBuf setBoolean(int index, boolean value) { public ByteBuf setBoolean(int index, boolean value) {
leak.record(); recordLeakNonRefCountingOperation();
return super.setBoolean(index, value); return super.setBoolean(index, value);
} }
@Override @Override
public ByteBuf setByte(int index, int value) { public ByteBuf setByte(int index, int value) {
leak.record(); recordLeakNonRefCountingOperation();
return super.setByte(index, value); return super.setByte(index, value);
} }
@Override @Override
public ByteBuf setShort(int index, int value) { public ByteBuf setShort(int index, int value) {
leak.record(); recordLeakNonRefCountingOperation();
return super.setShort(index, value); return super.setShort(index, value);
} }
@Override @Override
public ByteBuf setMedium(int index, int value) { public ByteBuf setMedium(int index, int value) {
leak.record(); recordLeakNonRefCountingOperation();
return super.setMedium(index, value); return super.setMedium(index, value);
} }
@Override @Override
public ByteBuf setInt(int index, int value) { public ByteBuf setInt(int index, int value) {
leak.record(); recordLeakNonRefCountingOperation();
return super.setInt(index, value); return super.setInt(index, value);
} }
@Override @Override
public ByteBuf setLong(int index, long value) { public ByteBuf setLong(int index, long value) {
leak.record(); recordLeakNonRefCountingOperation();
return super.setLong(index, value); return super.setLong(index, value);
} }
@Override @Override
public ByteBuf setChar(int index, int value) { public ByteBuf setChar(int index, int value) {
leak.record(); recordLeakNonRefCountingOperation();
return super.setChar(index, value); return super.setChar(index, value);
} }
@Override @Override
public ByteBuf setFloat(int index, float value) { public ByteBuf setFloat(int index, float value) {
leak.record(); recordLeakNonRefCountingOperation();
return super.setFloat(index, value); return super.setFloat(index, value);
} }
@Override @Override
public ByteBuf setDouble(int index, double value) { public ByteBuf setDouble(int index, double value) {
leak.record(); recordLeakNonRefCountingOperation();
return super.setDouble(index, value); return super.setDouble(index, value);
} }
@Override @Override
public ByteBuf setBytes(int index, ByteBuf src) { public ByteBuf setBytes(int index, ByteBuf src) {
leak.record(); recordLeakNonRefCountingOperation();
return super.setBytes(index, src); return super.setBytes(index, src);
} }
@Override @Override
public ByteBuf setBytes(int index, ByteBuf src, int length) { public ByteBuf setBytes(int index, ByteBuf src, int length) {
leak.record(); recordLeakNonRefCountingOperation();
return super.setBytes(index, src, length); return super.setBytes(index, src, length);
} }
@Override @Override
public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) { public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
leak.record(); recordLeakNonRefCountingOperation();
return super.setBytes(index, src, srcIndex, length); return super.setBytes(index, src, srcIndex, length);
} }
@Override @Override
public ByteBuf setBytes(int index, byte[] src) { public ByteBuf setBytes(int index, byte[] src) {
leak.record(); recordLeakNonRefCountingOperation();
return super.setBytes(index, src); return super.setBytes(index, src);
} }
@Override @Override
public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) { public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
leak.record(); recordLeakNonRefCountingOperation();
return super.setBytes(index, src, srcIndex, length); return super.setBytes(index, src, srcIndex, length);
} }
@Override @Override
public ByteBuf setBytes(int index, ByteBuffer src) { public ByteBuf setBytes(int index, ByteBuffer src) {
leak.record(); recordLeakNonRefCountingOperation();
return super.setBytes(index, src); return super.setBytes(index, src);
} }
@Override @Override
public int setBytes(int index, InputStream in, int length) throws IOException { public int setBytes(int index, InputStream in, int length) throws IOException {
leak.record(); recordLeakNonRefCountingOperation();
return super.setBytes(index, in, length); return super.setBytes(index, in, length);
} }
@Override @Override
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException { public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
leak.record(); recordLeakNonRefCountingOperation();
return super.setBytes(index, in, length); return super.setBytes(index, in, length);
} }
@Override @Override
public ByteBuf setZero(int index, int length) { public ByteBuf setZero(int index, int length) {
leak.record(); recordLeakNonRefCountingOperation();
return super.setZero(index, length); return super.setZero(index, length);
} }
@Override @Override
public boolean readBoolean() { public boolean readBoolean() {
leak.record(); recordLeakNonRefCountingOperation();
return super.readBoolean(); return super.readBoolean();
} }
@Override @Override
public byte readByte() { public byte readByte() {
leak.record(); recordLeakNonRefCountingOperation();
return super.readByte(); return super.readByte();
} }
@Override @Override
public short readUnsignedByte() { public short readUnsignedByte() {
leak.record(); recordLeakNonRefCountingOperation();
return super.readUnsignedByte(); return super.readUnsignedByte();
} }
@Override @Override
public short readShort() { public short readShort() {
leak.record(); recordLeakNonRefCountingOperation();
return super.readShort(); return super.readShort();
} }
@Override @Override
public int readUnsignedShort() { public int readUnsignedShort() {
leak.record(); recordLeakNonRefCountingOperation();
return super.readUnsignedShort(); return super.readUnsignedShort();
} }
@Override @Override
public int readMedium() { public int readMedium() {
leak.record(); recordLeakNonRefCountingOperation();
return super.readMedium(); return super.readMedium();
} }
@Override @Override
public int readUnsignedMedium() { public int readUnsignedMedium() {
leak.record(); recordLeakNonRefCountingOperation();
return super.readUnsignedMedium(); return super.readUnsignedMedium();
} }
@Override @Override
public int readInt() { public int readInt() {
leak.record(); recordLeakNonRefCountingOperation();
return super.readInt(); return super.readInt();
} }
@Override @Override
public long readUnsignedInt() { public long readUnsignedInt() {
leak.record(); recordLeakNonRefCountingOperation();
return super.readUnsignedInt(); return super.readUnsignedInt();
} }
@Override @Override
public long readLong() { public long readLong() {
leak.record(); recordLeakNonRefCountingOperation();
return super.readLong(); return super.readLong();
} }
@Override @Override
public char readChar() { public char readChar() {
leak.record(); recordLeakNonRefCountingOperation();
return super.readChar(); return super.readChar();
} }
@Override @Override
public float readFloat() { public float readFloat() {
leak.record(); recordLeakNonRefCountingOperation();
return super.readFloat(); return super.readFloat();
} }
@Override @Override
public double readDouble() { public double readDouble() {
leak.record(); recordLeakNonRefCountingOperation();
return super.readDouble(); return super.readDouble();
} }
@Override @Override
public ByteBuf readBytes(int length) { public ByteBuf readBytes(int length) {
leak.record(); recordLeakNonRefCountingOperation();
return super.readBytes(length); return super.readBytes(length);
} }
@Override @Override
public ByteBuf readBytes(ByteBuf dst) { public ByteBuf readBytes(ByteBuf dst) {
leak.record(); recordLeakNonRefCountingOperation();
return super.readBytes(dst); return super.readBytes(dst);
} }
@Override @Override
public ByteBuf readBytes(ByteBuf dst, int length) { public ByteBuf readBytes(ByteBuf dst, int length) {
leak.record(); recordLeakNonRefCountingOperation();
return super.readBytes(dst, length); return super.readBytes(dst, length);
} }
@Override @Override
public ByteBuf readBytes(ByteBuf dst, int dstIndex, int length) { public ByteBuf readBytes(ByteBuf dst, int dstIndex, int length) {
leak.record(); recordLeakNonRefCountingOperation();
return super.readBytes(dst, dstIndex, length); return super.readBytes(dst, dstIndex, length);
} }
@Override @Override
public ByteBuf readBytes(byte[] dst) { public ByteBuf readBytes(byte[] dst) {
leak.record(); recordLeakNonRefCountingOperation();
return super.readBytes(dst); return super.readBytes(dst);
} }
@Override @Override
public ByteBuf readBytes(byte[] dst, int dstIndex, int length) { public ByteBuf readBytes(byte[] dst, int dstIndex, int length) {
leak.record(); recordLeakNonRefCountingOperation();
return super.readBytes(dst, dstIndex, length); return super.readBytes(dst, dstIndex, length);
} }
@Override @Override
public ByteBuf readBytes(ByteBuffer dst) { public ByteBuf readBytes(ByteBuffer dst) {
leak.record(); recordLeakNonRefCountingOperation();
return super.readBytes(dst); return super.readBytes(dst);
} }
@Override @Override
public ByteBuf readBytes(OutputStream out, int length) throws IOException { public ByteBuf readBytes(OutputStream out, int length) throws IOException {
leak.record(); recordLeakNonRefCountingOperation();
return super.readBytes(out, length); return super.readBytes(out, length);
} }
@Override @Override
public int readBytes(GatheringByteChannel out, int length) throws IOException { public int readBytes(GatheringByteChannel out, int length) throws IOException {
leak.record(); recordLeakNonRefCountingOperation();
return super.readBytes(out, length); return super.readBytes(out, length);
} }
@Override @Override
public ByteBuf skipBytes(int length) { public ByteBuf skipBytes(int length) {
leak.record(); recordLeakNonRefCountingOperation();
return super.skipBytes(length); return super.skipBytes(length);
} }
@Override @Override
public ByteBuf writeBoolean(boolean value) { public ByteBuf writeBoolean(boolean value) {
leak.record(); recordLeakNonRefCountingOperation();
return super.writeBoolean(value); return super.writeBoolean(value);
} }
@Override @Override
public ByteBuf writeByte(int value) { public ByteBuf writeByte(int value) {
leak.record(); recordLeakNonRefCountingOperation();
return super.writeByte(value); return super.writeByte(value);
} }
@Override @Override
public ByteBuf writeShort(int value) { public ByteBuf writeShort(int value) {
leak.record(); recordLeakNonRefCountingOperation();
return super.writeShort(value); return super.writeShort(value);
} }
@Override @Override
public ByteBuf writeMedium(int value) { public ByteBuf writeMedium(int value) {
leak.record(); recordLeakNonRefCountingOperation();
return super.writeMedium(value); return super.writeMedium(value);
} }
@Override @Override
public ByteBuf writeInt(int value) { public ByteBuf writeInt(int value) {
leak.record(); recordLeakNonRefCountingOperation();
return super.writeInt(value); return super.writeInt(value);
} }
@Override @Override
public ByteBuf writeLong(long value) { public ByteBuf writeLong(long value) {
leak.record(); recordLeakNonRefCountingOperation();
return super.writeLong(value); return super.writeLong(value);
} }
@Override @Override
public ByteBuf writeChar(int value) { public ByteBuf writeChar(int value) {
leak.record(); recordLeakNonRefCountingOperation();
return super.writeChar(value); return super.writeChar(value);
} }
@Override @Override
public ByteBuf writeFloat(float value) { public ByteBuf writeFloat(float value) {
leak.record(); recordLeakNonRefCountingOperation();
return super.writeFloat(value); return super.writeFloat(value);
} }
@Override @Override
public ByteBuf writeDouble(double value) { public ByteBuf writeDouble(double value) {
leak.record(); recordLeakNonRefCountingOperation();
return super.writeDouble(value); return super.writeDouble(value);
} }
@Override @Override
public ByteBuf writeBytes(ByteBuf src) { public ByteBuf writeBytes(ByteBuf src) {
leak.record(); recordLeakNonRefCountingOperation();
return super.writeBytes(src); return super.writeBytes(src);
} }
@Override @Override
public ByteBuf writeBytes(ByteBuf src, int length) { public ByteBuf writeBytes(ByteBuf src, int length) {
leak.record(); recordLeakNonRefCountingOperation();
return super.writeBytes(src, length); return super.writeBytes(src, length);
} }
@Override @Override
public ByteBuf writeBytes(ByteBuf src, int srcIndex, int length) { public ByteBuf writeBytes(ByteBuf src, int srcIndex, int length) {
leak.record(); recordLeakNonRefCountingOperation();
return super.writeBytes(src, srcIndex, length); return super.writeBytes(src, srcIndex, length);
} }
@Override @Override
public ByteBuf writeBytes(byte[] src) { public ByteBuf writeBytes(byte[] src) {
leak.record(); recordLeakNonRefCountingOperation();
return super.writeBytes(src); return super.writeBytes(src);
} }
@Override @Override
public ByteBuf writeBytes(byte[] src, int srcIndex, int length) { public ByteBuf writeBytes(byte[] src, int srcIndex, int length) {
leak.record(); recordLeakNonRefCountingOperation();
return super.writeBytes(src, srcIndex, length); return super.writeBytes(src, srcIndex, length);
} }
@Override @Override
public ByteBuf writeBytes(ByteBuffer src) { public ByteBuf writeBytes(ByteBuffer src) {
leak.record(); recordLeakNonRefCountingOperation();
return super.writeBytes(src); return super.writeBytes(src);
} }
@Override @Override
public int writeBytes(InputStream in, int length) throws IOException { public int writeBytes(InputStream in, int length) throws IOException {
leak.record(); recordLeakNonRefCountingOperation();
return super.writeBytes(in, length); return super.writeBytes(in, length);
} }
@Override @Override
public int writeBytes(ScatteringByteChannel in, int length) throws IOException { public int writeBytes(ScatteringByteChannel in, int length) throws IOException {
leak.record(); recordLeakNonRefCountingOperation();
return super.writeBytes(in, length); return super.writeBytes(in, length);
} }
@Override @Override
public ByteBuf writeZero(int length) { public ByteBuf writeZero(int length) {
leak.record(); recordLeakNonRefCountingOperation();
return super.writeZero(length); return super.writeZero(length);
} }
@Override @Override
public int indexOf(int fromIndex, int toIndex, byte value) { public int indexOf(int fromIndex, int toIndex, byte value) {
leak.record(); recordLeakNonRefCountingOperation();
return super.indexOf(fromIndex, toIndex, value); return super.indexOf(fromIndex, toIndex, value);
} }
@Override @Override
public int bytesBefore(byte value) { public int bytesBefore(byte value) {
leak.record(); recordLeakNonRefCountingOperation();
return super.bytesBefore(value); return super.bytesBefore(value);
} }
@Override @Override
public int bytesBefore(int length, byte value) { public int bytesBefore(int length, byte value) {
leak.record(); recordLeakNonRefCountingOperation();
return super.bytesBefore(length, value); return super.bytesBefore(length, value);
} }
@Override @Override
public int bytesBefore(int index, int length, byte value) { public int bytesBefore(int index, int length, byte value) {
leak.record(); recordLeakNonRefCountingOperation();
return super.bytesBefore(index, length, value); return super.bytesBefore(index, length, value);
} }
@Override @Override
public int forEachByte(ByteBufProcessor processor) { public int forEachByte(ByteBufProcessor processor) {
leak.record(); recordLeakNonRefCountingOperation();
return super.forEachByte(processor); return super.forEachByte(processor);
} }
@Override @Override
public int forEachByte(int index, int length, ByteBufProcessor processor) { public int forEachByte(int index, int length, ByteBufProcessor processor) {
leak.record(); recordLeakNonRefCountingOperation();
return super.forEachByte(index, length, processor); return super.forEachByte(index, length, processor);
} }
@Override @Override
public int forEachByteDesc(ByteBufProcessor processor) { public int forEachByteDesc(ByteBufProcessor processor) {
leak.record(); recordLeakNonRefCountingOperation();
return super.forEachByteDesc(processor); return super.forEachByteDesc(processor);
} }
@Override @Override
public int forEachByteDesc(int index, int length, ByteBufProcessor processor) { public int forEachByteDesc(int index, int length, ByteBufProcessor processor) {
leak.record(); recordLeakNonRefCountingOperation();
return super.forEachByteDesc(index, length, processor); return super.forEachByteDesc(index, length, processor);
} }
@Override @Override
public ByteBuf copy() { public ByteBuf copy() {
leak.record(); recordLeakNonRefCountingOperation();
return super.copy(); return super.copy();
} }
@Override @Override
public ByteBuf copy(int index, int length) { public ByteBuf copy(int index, int length) {
leak.record(); recordLeakNonRefCountingOperation();
return super.copy(index, length); return super.copy(index, length);
} }
@Override @Override
public int nioBufferCount() { public int nioBufferCount() {
leak.record(); recordLeakNonRefCountingOperation();
return super.nioBufferCount(); return super.nioBufferCount();
} }
@Override @Override
public ByteBuffer nioBuffer() { public ByteBuffer nioBuffer() {
leak.record(); recordLeakNonRefCountingOperation();
return super.nioBuffer(); return super.nioBuffer();
} }
@Override @Override
public ByteBuffer nioBuffer(int index, int length) { public ByteBuffer nioBuffer(int index, int length) {
leak.record(); recordLeakNonRefCountingOperation();
return super.nioBuffer(index, length); return super.nioBuffer(index, length);
} }
@Override @Override
public ByteBuffer[] nioBuffers() { public ByteBuffer[] nioBuffers() {
leak.record(); recordLeakNonRefCountingOperation();
return super.nioBuffers(); return super.nioBuffers();
} }
@Override @Override
public ByteBuffer[] nioBuffers(int index, int length) { public ByteBuffer[] nioBuffers(int index, int length) {
leak.record(); recordLeakNonRefCountingOperation();
return super.nioBuffers(index, length); return super.nioBuffers(index, length);
} }
@Override @Override
public ByteBuffer internalNioBuffer(int index, int length) { public ByteBuffer internalNioBuffer(int index, int length) {
leak.record(); recordLeakNonRefCountingOperation();
return super.internalNioBuffer(index, length); return super.internalNioBuffer(index, length);
} }
@Override @Override
public String toString(Charset charset) { public String toString(Charset charset) {
leak.record(); recordLeakNonRefCountingOperation();
return super.toString(charset); return super.toString(charset);
} }
@Override @Override
public String toString(int index, int length, Charset charset) { public String toString(int index, int length, Charset charset) {
leak.record(); recordLeakNonRefCountingOperation();
return super.toString(index, length, charset); return super.toString(index, length, charset);
} }
@ -718,7 +732,7 @@ final class AdvancedLeakAwareByteBuf extends WrappedByteBuf {
@Override @Override
public ByteBuf capacity(int newCapacity) { public ByteBuf capacity(int newCapacity) {
leak.record(); recordLeakNonRefCountingOperation();
return super.capacity(newCapacity); return super.capacity(newCapacity);
} }
} }

View File

@ -33,9 +33,14 @@ import static io.netty.util.internal.StringUtil.*;
public final class ResourceLeakDetector<T> { public final class ResourceLeakDetector<T> {
private static final String PROP_LEVEL = "io.netty.leakDetectionLevel"; private static final String PROP_LEVEL_OLD = "io.netty.leakDetectionLevel";
private static final String PROP_LEVEL = "io.netty.leakDetection.level";
private static final Level DEFAULT_LEVEL = Level.SIMPLE; private static final Level DEFAULT_LEVEL = Level.SIMPLE;
private static final String PROP_MAX_RECORDS = "io.netty.leakDetection.maxRecords";
private static final int DEFAULT_MAX_RECORDS = 4;
private static final int MAX_RECORDS;
/** /**
* Represents the level of resource leak detection. * Represents the level of resource leak detection.
*/ */
@ -78,7 +83,12 @@ public final class ResourceLeakDetector<T> {
} }
Level defaultLevel = disabled? Level.DISABLED : DEFAULT_LEVEL; Level defaultLevel = disabled? Level.DISABLED : DEFAULT_LEVEL;
String levelStr = SystemPropertyUtil.get(PROP_LEVEL, defaultLevel.name()).trim().toUpperCase();
// First read old property name
String levelStr = SystemPropertyUtil.get(PROP_LEVEL_OLD, defaultLevel.name()).trim().toUpperCase();
// If new property name is present, use it
levelStr = SystemPropertyUtil.get(PROP_LEVEL, levelStr).trim().toUpperCase();
Level level = DEFAULT_LEVEL; Level level = DEFAULT_LEVEL;
for (Level l: EnumSet.allOf(Level.class)) { for (Level l: EnumSet.allOf(Level.class)) {
if (levelStr.equals(l.name()) || levelStr.equals(String.valueOf(l.ordinal()))) { if (levelStr.equals(l.name()) || levelStr.equals(String.valueOf(l.ordinal()))) {
@ -86,9 +96,12 @@ public final class ResourceLeakDetector<T> {
} }
} }
MAX_RECORDS = SystemPropertyUtil.getInt(PROP_MAX_RECORDS, DEFAULT_MAX_RECORDS);
ResourceLeakDetector.level = level; ResourceLeakDetector.level = level;
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("-D{}: {}", PROP_LEVEL, level.name().toLowerCase()); logger.debug("-D{}: {}", PROP_LEVEL, level.name().toLowerCase());
logger.debug("-D{}: {}", PROP_MAX_RECORDS, MAX_RECORDS);
} }
} }
@ -252,9 +265,6 @@ public final class ResourceLeakDetector<T> {
} }
private final class DefaultResourceLeak extends PhantomReference<Object> implements ResourceLeak { private final class DefaultResourceLeak extends PhantomReference<Object> implements ResourceLeak {
private static final int MAX_RECORDS = 4;
private final String creationRecord; private final String creationRecord;
private final Deque<String> lastRecords = new ArrayDeque<String>(); private final Deque<String> lastRecords = new ArrayDeque<String>();
private final AtomicBoolean freed; private final AtomicBoolean freed;
@ -357,6 +367,7 @@ public final class ResourceLeakDetector<T> {
private static final String[] STACK_TRACE_ELEMENT_EXCLUSIONS = { private static final String[] STACK_TRACE_ELEMENT_EXCLUSIONS = {
"io.netty.buffer.AbstractByteBufAllocator.toLeakAwareBuffer(", "io.netty.buffer.AbstractByteBufAllocator.toLeakAwareBuffer(",
"io.netty.buffer.AdvancedLeakAwareByteBuf.recordLeakNonRefCountingOperation("
}; };
static String newRecord(int recordsToSkip) { static String newRecord(int recordsToSkip) {