【发布时间】:2015-03-04 11:12:38
【问题描述】:
我对 Netty 5(或 4)中的引用计数 bytebuf 有一些疑问。我发现当一个bytebuf超出生命周期时我不释放它什么都没有发生,看来bytebuf使用的内存可以被正确GCed。
在此链接 http://netty.io/wiki/reference-counted-objects.html 中,它说设置 JVM 选项 '-Dio.netty.leakDetectionLevel=advanced' 或调用 ResourceLeakDetector.setLevel() 可以检测资源泄漏,但我无法使用下面的代码重现它。
public class App {
public static ByteBuf a(ByteBuf input) {
input.writeByte(42);
return input;
}
public static ByteBuf b(ByteBuf input) {
try {
ByteBuf output;
output = input.alloc().directBuffer(input.readableBytes() + 1);
output.writeBytes(input);
output.writeByte(42);
return output;
} finally {
// input.release();
}
}
public static void c(ByteBuf input) {
//System.out.println(input);
// input.release();
}
static class Task implements Runnable {
ByteBuf bbBuf;
public Task(ByteBuf buf) {
bbBuf = buf;
}
public void run() {
c(b(a(bbBuf)));
}
}
public static void main(String[] args) {
ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.PARANOID);
AbstractByteBufAllocator allocator = new PooledByteBufAllocator();
ByteBuf buf = allocator.buffer(10, 100);
// buf.release();
new Thread(new Task(buf)).start();
System.out.println(buf.refCnt());
assert buf.refCnt() == 0;
}
}
那么问题出在哪里?
【问题讨论】:
标签: netty