如果从ChannelHandlerContext分配,PooledByteBufAllocator的缓冲区将在HttpServerCodecHandler中释放?

时间:2014-03-28 03:17:02

标签: netty

我使用PooledByteBufAllocator和Direct Pool分配缓冲区为true,这是在服务器设置期间在通道中指定的分配器。我使用此分配器来创建响应。我是否需要在响应中释放此缓冲区,或者是否将在HttpServerCodec中处理此响应缓冲池的释放?我这样做是为了获得性能优势而不是gc。

管道代码:

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

ServerBootstrap代码:

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();

在响应代码中,在我的自定义'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);


}

1 个答案:

答案 0 :(得分:0)

如果你写了回复,Netty会注意为你释放缓冲区。