Interface ByteBufferPool

All Known Implementing Classes:
ArrayByteBufferPool, ArrayByteBufferPool.Quadratic, ArrayByteBufferPool.Tracking, ByteBufferPool.NonPooling, ByteBufferPool.Wrapper

public interface ByteBufferPool

A pool for RetainableByteBuffer instances.

RetainableByteBuffer that are acquired must be released by calling Retainable.release() otherwise the memory they hold will be leaked.

API NOTE

This interface does not have a symmetric release(RetainableByteBuffer) method, because it will be confusing to use due to the fact that the acquired instance is-a Retainable.

Imagine this (hypothetical) code sequence:


 RetainableByteBuffer buffer = pool.acquire(size, direct);
 buffer.retain();
 pool.release(buffer);
 

The hypothetical call to release(RetainableByteBuffer) would appear to release the buffer to the pool, but in fact the buffer is retained one more time (and therefore still in use) and not really released to the pool. For this reason there is no release(RetainableByteBuffer) method.

Therefore, in order to track acquire/release counts both the pool and the buffer returned by acquire(int, boolean) must be wrapped, see RetainableByteBuffer.Wrapper

  • Method Details

    • acquire

      RetainableByteBuffer acquire(int size, boolean direct)

      Acquires a RetainableByteBuffer from this pool.

      Parameters:
      size - The size of the buffer. The returned buffer will have at least this capacity.
      direct - true if a direct memory buffer is needed, false otherwise.
      Returns:
      a RetainableByteBuffer with position and limit set to 0.
    • clear

      void clear()

      Removes all non-retained pooled instances from this pool.