1

Considering an application where you need a high-level API to return a ByteBuffer of any specified size, what is the most effective way to pool these buffers? Are there any examples? Thread safety is not a concern.

Jire
  • 9,680
  • 14
  • 52
  • 87
  • Can you give an example of the API? Right now, it's hard to tell what you're asking. You want a pool of unused objects that were already allocated, but are now unused? I'm assuming you don't want to pay the time cost of recreating things over and over again, but everything about this is.... odd. – Dean J Jul 12 '15 at 22:32
  • Or, you'd normally keep the instance variables around that you expect to need again, and just reuse them. Having multiple classes and methods all needing the same size buffer makes me wonder if they should be the same class, or just share a single buffer object, but that still wouldn't need a pool of unused objects. What are you trying to do? :) – Dean J Jul 12 '15 at 22:34
  • @DeanJ I want to be able to get a buffer of any size using minimal amounts of actual buffers (keeping buffer creation to a minimal). I believe this can be done with `ByteBuffer.slice` but I'm unsure how to implement. – Jire Jul 12 '15 at 22:59
  • You can do it with *one* `ByteBuffer` and `slice()`, but it's hard to see the point. You just give yourself the same GC headache that GC already solves. – user207421 Jul 13 '15 at 00:35
  • @EJP It's not a headache since I use one thread. Acquire the buffer and release it back to the pool afterward. – Jire Jul 13 '15 at 01:03
  • Unless you've got a specific example to share of why you'd ever want to do this, or can make the question more specific, there's no good answer here; you're reinventing functionality that Java already provides. Anything you create will be worse-performing than Java's implementation, and you haven't given enough specifics to show what you'd rather optimize for. – Dean J Jul 14 '15 at 20:38

0 Answers0