netty 4 LEAK:ByteBuf在被正确释放之前已经GC了

时间:2016-03-04 21:47:49

标签: java netty decoder

我的netty服务器每天会收到一次或两次此警告,而解码方法每天处理超过800个数据包。

它似乎不喜欢的行是super.decode()行,但我不确定这是因为buffer变量(本地变量)还是buf变量(我收到的bytebuf。)

我在release()区块中添加了finally,但警告仍然存在。

我正在使用netty 4.0.4 final。

WARN  ResourceLeakDetector - LEAK: ByteBuf was GC'd before being released correctly.  The following stack trace shows where the leaked object was created, rather than where you failed to release it.
io.netty.util.ResourceLeakException: io.netty.buffer.UnpooledUnsafeDirectByteBuf@13e5013
    at io.netty.util.ResourceLeakDetector$DefaultResourceLeak.<init>(ResourceLeakDetector.java:174)
    at io.netty.util.ResourceLeakDetector.open(ResourceLeakDetector.java:116)
    at io.netty.buffer.UnpooledUnsafeDirectByteBuf.<init>(UnpooledUnsafeDirectByteBuf.java:72)
    at io.netty.buffer.UnpooledByteBufAllocator.newDirectBuffer(UnpooledByteBufAllocator.java:49)
    at io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:132)
    at io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:123)
    at io.netty.buffer.AbstractByteBufAllocator.buffer(AbstractByteBufAllocator.java:60)
    at io.netty.handler.codec.LengthFieldBasedFrameDecoder.extractFrame(LengthFieldBasedFrameDecoder.java:486)
    at io.netty.handler.codec.LengthFieldBasedFrameDecoder.decode(LengthFieldBasedFrameDecoder.java:425)
    at PacketDecoder.decode(PacketDecoder.java:62)
    at io.netty.handler.codec.LengthFieldBasedFrameDecoder.decode(LengthFieldBasedFrameDecoder.java:351)
    at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:231)
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:131)
    at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:368)
    at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:353)
    at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:780)
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:100)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:497)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:465)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:359)
    at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:101)
    at java.lang.Thread.run(Thread.java:745)

我的代码:

@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf buf) throws Exception {
    LOGGER.info(" in the decode handler ");
    ByteBuf buffer = ((ByteBuf) super.decode(ctx, buf)).order(ByteOrder.BIG_ENDIAN);
    if (buffer == null) {
        LOGGER.error(" not all the message is received or a null message ");
        return null;
    }
    try {

        // reading the buffer until the end 
        // return something other than null     
        }

    }
    catch ( Exception e ){
        LOGGER.error(e.getMessage() );
        e.printStackTrace();
        return null;
    }
     finally {
         buffer.retain();
         buffer.release();
        }

}

1 个答案:

答案 0 :(得分:0)

好像你并不总是调用ByteBuf.release()。您应确保始终释放缓冲区。