简体   繁体   中英

Where is the memory of direct buffer allocated by netty, kernel space or user space?

I write a simple http client using netty 4. As far as I know, if I use HeapBuffer, when I get the ByteBuffer in the ChannelInboundHandler

@Override
public void channelRead(ChannelHandlerContext ctx, final ByteBuf msg) throws Exception {
    // do nothing with the msg
}

The bytes from the website should be a process like (even if I don't call ByteBuf.read)

socket -> kernel space -> user space

The bytes should have been in the java heap, right?

I want to know if I use DirectBuffer, when the "channelRead" triggered, where are the bytes form the website if I don't call ByteBuf.read, are they in the kernel space, or in the user space? Is it one less memory-copy than I use HeapBuffer (because it won't copy to the user space)?

A DirectBuffer is allocated in user space, but outside the JVM heap. Note that when talking about socket I/O, this still involves a copy from the kernel buffers to the user space buffers. However, it does eliminate the need for intermediate user-space buffers that the JVM would need to use to copy to/from the heap. As such, I/O operations can happen directly on the native user-space buffer, as if you were running C code.

Re: your netty code snippet, by the time netty calls channelRead , data has already been read into the ByteBuf (in user-space). If you want the data to remain in socket buffers (for example, to implement backpressure), you can call channel.config.setAutoRead(false) to tell netty not to read from the socket buffers automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM