为什么虽然我在finally块中释放了缓冲区,但为什么仍得到netty.util.ResourceLeakDetector?

时间:2019-07-18 17:22:40

标签: netty

这是我代码的要旨

public static void doSomething(ByteBuf buf) {
        ByteBuf bufCopy;
        try {
            bufCopy = buf.copy();
            byte[] bytes = new byte[buf.readableBytes()];
            buf.readBytes(bytes);
            try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes)) {
              //do something
            } catch (IOException ex) {
                throw new IllegalArgumentException("something went wrong", ex);
            }
        }
        finally {
            buf.release();
        }
    }

我在日志中收到以下消息

io.netty.util.ResourceLeakDetector       : LEAK: ByteBuf.release() was not called before it's garbage-collected. See http://netty.io/wiki/reference-counted-objects.html for more information.
Recent access records: 
Created at:
    io.netty.buffer.PooledByteBufAllocator.newDirectBuffer(PooledByteBufAllocator.java:331)
    io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:185)
    io.netty.buffer.UnsafeByteBufUtil.copy(UnsafeByteBufUtil.java:436)
    io.netty.buffer.PooledUnsafeDirectByteBuf.copy(PooledUnsafeDirectByteBuf.java:309)
    io.netty.buffer.AbstractByteBuf.copy(AbstractByteBuf.java:1194)

这里有什么问题以及如何解决?

顺便说一句,总是可以重复的。重新启动服务器时,错误消失了,但是一旦服务器启动并运行了几分钟,我开始看到此警告。

1 个答案:

答案 0 :(得分:0)

因为您也错过了发布bufCopy的机会。这在日志中清楚显示。