I have a problem with Netty direct memory management.
I create a direct buffer:
@GetMapping("/new/buffer/netty/unpool") @Synchronized public void newBufferNetty() { UnpooledByteBufAllocator allocator = UnpooledByteBufAllocator.DEFAULT; ByteBuf buffer = allocator.buffer(100 * 1024 * 1024); _myByteBufList.add(buffer); log.info("buffer[{}] created", buffer); }
Then I noticed the information generated by top , and I found that there were no changes in memory (RES, SWAP, and free). I was confused because everything is fine (OS memory information will change) if I do this in NIO, like ByteBuffer.allocateDirect(1024*1024*100); .
After I examined the source code, I found that NIO creates directByteBuffer using new DirectByteBuffer(cap) , and Netty actually creates it using new DirectByteBuffer(addr, cap) . In the latter case, Netty did not call Bits.reserve(size, cap) , so I think why there are no changes shown by top .
And I also found that Netty uses its own DIRECT_MEMORY_COUNTER counter to track the direct memory it allocates.
My question is:
Why is Netty not called by Bits.reserve when allocating direct memory?
Why should Netty use its own counter to monitor direct memory usage?
(this is the one that bothers me more). Why there are no changes in os memory when creating a Netty buffer (UnpooledUnsafeNoCleanerDirectByteBuf)
Thank you in advance.
source share