javanettynionetty4direct-buffer

How to release Pooled direct ByteBuf in netty when written after by ChannelHandlerContext?


In my c/s project, c/s need to exchange fixed-size message with netty.
I use pooled direct buffer to buffer message.
Code just like below:

public void channelRead(ChannelHandlerContext ctx, Object msg) {
    ByteBuf in = (ByteBuf) msg;
    try {
        ...
    } finally {
        ReferenceCountUtil.release(msg);
    }
    // alloc larger size of memory to increase memory usage
    ByteBuf buffer = PooledByteBufAllocator.DEFAULT.directBuffer(4* 1024 * 1024, 4* 1024 * 1024);
    // mock exchange message
    for (int i = 0; i < 1024 * 1024; i++) {
        buffer.writeInt(i);
    }
    // code 1
    // ctx.write(buffer);
    // ReferenceCountUtil.release(buffer);
    // code 2
    // ctx.write(buffer, ctx.newPromise().addListener(f -> ReferenceCountUtil.release(buffer)));
    // code 3
    // ctx.write(buffer);
}

As I know, I need to release ByteBuf.
Which is the right way to use buffer, code 1, code 2 or code 3?
Intuitively code 2 may be a good choice.
But code 2 occurs error,

i.n.util.concurrent.DefaultPromise - An exception was thrown by XXXServerHandler$$Lambda$73/0x000000080018ac40.operationComplete()
io.netty.util.IllegalReferenceCountException: refCnt: 0, decrement: 1

Thanks.


Solution

  • You need to do the option 3 "code 3" The handler on your channel pipeline is already calling release, when it is done with the bytebuf.