2

Say, we declare in MyClass:

ByteBuf fixedBuf = Unpooled.unreleasableBuffer(
                       PooledByteBufAllocator.DEFAULT.directBuffer(64, 64));

This should reserve a 64 bytes chunk of off-heap memory. As documented, Netty ignores here the 'release()' and 'retain()' functionality, therefore, the chunk remains allocated throughout the life of MyClass. What will happen to the allocated off-heap memory chunk once the instance of MyClass gets out of scope? Will JVM/GC release it? If not, or if I want to discard the 'fixedBuf ' programatically, how can I best deallocate the claimed memory blocks for such ByteBuf objects?

Would you please also point to explanation(s) / example(s) / best practice for avoiding any memory leaks and issues on ByteBuf objects?

Felix
  • 1,662
  • 2
  • 18
  • 37

1 Answers1

1

(JVM only) It appears that the Direct Buffer will be cleaned up if no references are to it by default in the JVM during the heap GC but not effected by heap pressure so manual cleaning may be required as per previous stack overflow questions. Stack Overflow 1, Stack Overflow 2

(For Netty) Since Netty references the Direct Buffer I would think that it does not clean it until the PoolThreadCache runs the method free() which manually uses the Netty Internal/Cleaners which happens in finalize block of the PoolThreadCache.java and a other locations. If you are going out of scope it should clean automatically when the PoolThreadCache.java uses free; but I have not tracked down when that happens. If in doubt use Unpooled.unwrap() and release it yourself.

Some working notes posted here at netty/netty/issues/8139 where I show the steps of finding the answer using their source code. Is there a need to free memory of Unpooled.unreleasableBuffer #8139

Mr00Anderson
  • 854
  • 8
  • 16