yang yang
yang yang

Reputation: 169

use netty allocator to creat bytebuf per channel or per request?

hi I got a netty server to accept massive tcp connect .

After read the document,

I am not sure about:

  1. should I allocate one bytebuf for each channel after initiated?

  2. or should I allocate one bytebuf for each request?

In official example it write like this, but I feel strange, if I create bytebuf for each read operation, then what is the purpose to do that?

If I allocate a bytebuf for each channel, will it gain more performance advantage?

 @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
        ByteBuf buff = ctx.alloc().buffer();
        buff.writeBytes(XX);
        ctx.writeAndFlush(buff);
}

Upvotes: 0

Views: 752

Answers (1)

Norman Maurer
Norman Maurer

Reputation: 23567

You will allocate a new buffer whenever you need it as you do not really have a good idea on when you could re-use the buffer by yourself.

That said netty uses the PooledByteBufAllocator by default which means buffers are pooled so allocation is not really so expensive here.

Upvotes: 1

Related Questions