0

When we create a ByteBuffer and then continually reuse it Java does no GC on it.

Netty provides ByteBuffers for its input and output.

Does it continually reuse these, by keeping an internal positioning, so that the buffers never need garbage collection?

Or must something special be done by the implementor to ensure no GC?

BAR
  • 15,909
  • 27
  • 97
  • 185
  • 1
    Recycling the buffers reduces the garbage but netty doesn't eliminate it, it tries to reduce it to the point where it no longer has a significant impact on performance. – Peter Lawrey Feb 06 '15 at 14:45
  • Right, typically it would be worse performance wise to have a program with zero GC, rather than allowing a few objects to be created and discarded as necessary. My post is in reference to the IO memory space. I contend that position tracking etc. would be out of scope for this post. – BAR Feb 06 '15 at 15:08
  • What do you mean by "internal positioning"? AFAIK it keeps an ObjectPool. – Peter Lawrey Feb 06 '15 at 15:59
  • @PeterLawrey Anything like that. – BAR Feb 06 '15 at 16:27

1 Answers1

1

Netty 4 uses reference counting to manage the life cycle of ByteBuf. When a pooled buffer is released, the underlying memory region is returned to the pool, so that the memory region is not garbage collected. When an unpooled buffer is released, the underlying memory region is deallocated explicitly, or set to null so that JVM cleans it up later. It is recommended to use a pooled buffer of course.

Reference counting has an advantage over garbage collection:

  • It is possible to implement a buffer pool or allocator.
  • It is possible to deallocate off-heap buffers on time.

We hoped to implement a buffer pool or deallocate off-heap buffers on time without reference counting, but there's no way to get notified as soon as an object becomes unreachable in JVM.

trustin
  • 12,231
  • 6
  • 42
  • 52
  • Why not simply clear the byte buf, or use it as a circular byte buf? – BAR Feb 07 '15 at 03:40
  • Hi Trustin, I dont think, that getting notified once an object becomes unreachable would not help, either. Thats because these detections are not deterministic, as well as GC itself. So you would end up waiting for notification, while RAM is filling up. Reference counting is the only sane technique here. In C++ you have no other choice, either. Smart pointers are really just referenced counted pointers. That's totally fine in my opinion. Stay tuned for Java 9, Unsafe will probably be part of the JDK then =). – Kr0e Mar 20 '15 at 11:53
  • Actually you can use WeakReferences or SoftReferences and on creation time you can provide a ReferenceQueue which can notify you when a new reference has become unreachable. See http://stackoverflow.com/questions/14450538/using-javas-referencequeue#14450693 for more information. – laughing_man Jan 12 '17 at 08:08