PooledByteBufAllocator's buffer will released in HttpServerCodecHandler if allocated from ChannelHandlerContext?

StackOverflow https://stackoverflow.com/questions/22703538

  •  23-06-2023
  •  | 
  •  

Question

I am allocating a buffer using PooledByteBufAllocator with Direct Pool are true , which is the allocator specified in channel , during server setup. I use this allocator to create a response. Do I need to release this buffer in response or will release of this response buffer pool be handled in HttpServerCodec? I am doing this to get performance benefits and less gc.

Pipeline code:

pipeline.addLast("decoderEncoder", new HttpServerCodec());
pipeline.addLast("aggregator", new HttpObjectAggregator(1024 * 1024));// 1 MB data size
pipeline.addLast("handler", httpRequestHandlerProvider.get());

ServerBootstrap code:

serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            .localAddress(new InetSocketAddress(8800)).childHandler(serverChannelInitializer)
            // disable nagle's algorithm
            .childOption(ChannelOption.TCP_NODELAY, true)
            // allow binding channel on same ip, port
            .childOption(ChannelOption.SO_REUSEADDR, true).childOption(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(true)).bind()
            .sync();

In Response code , in my custom 'handler' - httpRequestHandler:

private void sendResp(final String responseString, final ChannelHandlerContext ctx) {
    byte[] bytes = responseString.getBytes(Charsets.UTF_8);

    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
            ctx.alloc().buffer(bytes.length).writeBytes(bytes), false);


}
Was it helpful?

Solution

If you write the response Netty will take care to release to buffer for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top