0

In order to reduce GC pauses in Java, I want to recycle objects of a particular class, to the best of my knowledge this can be done by using io.netty.util.Recycler. Unfortunately using io.netty.util.Recycler you need to call recycle each time the object is no longer referenced and in my case objects of this class are passed around throughout the code and it is difficult to keep track of where their life span ends and it makes the code quite ugly.

Does anyone know of a better method to recycle objects in Java, without having to keep track of where the objects are no longer referenced?

Project:- https://github.com/yahoo/pulsar

Class I want to recycle:- https://github.com/yahoo/pulsar/blob/master/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/PositionImpl.java

user1918858
  • 1,202
  • 1
  • 20
  • 29
  • 1
    Your problem description reads like the reason why garbage collection was invented: manually recycling objects can be difficult to implement, make your code hard to read and lead to subtle bugs (if you recycle objects that are still in use). But there is a solution that does _recycle objects in Java, without having to keep track of where the objects are no longer referenced_: it's called the garbage collector, and the best thing about is: you don't have to do anything to use it. – Thomas Kläger May 09 '17 at 05:20
  • The object you have there is immutable and small. It's probably not worth recycling it. Avoiding duplicate allocations maybe, but that's interning, not recycling. – the8472 May 09 '17 at 11:08
  • Indeed, it's probably not worth recycling that class. Minimizing GC pauses is probably best achieved by tuning the GC for your exact workload or better yet designing your code to create as little objects as possible. In cases like your's specifically I typically just bite the bullet and create new instances. – RecursiveExceptionException Jan 26 '18 at 16:35

0 Answers0