Search code examples
javareal-timenettyniolow-latency

Why Netty uses reference counting instead of just one ByteBuffer per connection when NIO is single-threaded?


I am confused why Netty 5.0 makes you use reference counting for ByteBuffers. Isn't Java NIO supposed to be single-threaded, in other words, one selector thread for many connections? Each client needs its own ByteBuffer and that's it, no pooling should be needed unless I am missing something.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    ByteBuf m = (ByteBuf) msg; // (1)
    try {
        long currentTimeMillis = (m.readUnsignedInt() - 2208988800L) * 1000L;
        System.out.println(new Date(currentTimeMillis));
        ctx.close();
    } finally {
        m.release();
    }
}

Source: http://netty.io/wiki/user-guide-for-5.x.html


Solution

  • It's because we don't know exactly what people will do with the ByteBuf. Also we support writing from different threads etc.