Search code examples
javamemorynettyheap-memorydirectmemory

Why it is recommended to use only Heap Buffers with [byte] operations in ChannelOutboundHandler.?


We are working on open-sourcing an api gateway and using Netty as underlying framework.

I came across Norman Maurer slides. In one of the slides, he mentioned

Only use heap buffers if need to operate on byte[] in ChannelOutboundHandler! By default direct ByteBuf will be returned by ByteBufAllocator.buffer(...).

Take this as rule of thumb

What is the reason behind this thumb of rule ?


Solution

  • The important part is "if need to operate on byte[]". As only when using heap ByteBufs it's possible to use byteBuf.array() to access the underlying byte[] without doing and extra memory copy.