Whats the exact purpose of releasing netty's message?

Viewed 134

What does releasing a message ((ByteBuf) msg).release(); exactly do in netty and what happens if we don't release message like the code below:

    @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    //received message
    ByteBuf in = (ByteBuf) msg;
    
    //message sent to remote peer without releasing
    ctx.writeAndFlush(msg);
}
1 Answers

Basically the "consumer" of the ByteBuf is responsible for releasing it. In your example the consumer is ctx which will hand the buffer over to the previous handler and so on. At the end it will hit the "transport itself" which will release the buffer once it was written.

If the "consumer" of the ByteBuf miss to release the buffer and not hand over the buffer to another component it will leak. As netty uses a pool for ByteBuf it means that at some point you will run out of memory.

Related