Search code examples
javamemorynettydirect-buffer

Direct Buffer used by ByteBuf is not fully released


I am using Netty 4.1.17-Final with Direct Buffer.

We send and receive 100 MB of byte[] in the test program, and when decoder reaches 100 MB, we discard it without doing anything.

@Override
public void decode(ByteBuffer _in, List<Object> _out) {
    if (_in.remaining() == size) {   // size = 100 * 1024 * 1024
        _in.get(new byte[size]);
    }
}

I explicitly executed GC at the end of the test and tried sleeping for about 10 seconds in anticipation of Cleaner releasing. However, Direct Buffer was not partially released, but Netty did not report a leak in ByteBuf.

Direct Buffer graph link

I have given the following options to the test program.
-Dio.netty.maxDirectMemory=0 -Dio.netty.leakDetection.level=PARANOID

I asked related questions below.
PooledUnsafeDirectByteBuf not found in DirectMemory


Solution

  • By default netty pools buffers so if you use the default allocator this is the case here.

    If you don't want to pool you should use UnpooledByteBufAllocator. This can be configured during bootstrapping with ChannelOption.ALLOCATOR.