Reputation: 169
hi I got a netty server to accept massive tcp connect .
After read the document,
I am not sure about:
should I allocate one bytebuf for each channel after initiated?
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
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