Interface ByteBufferPool

All Known Implementing Classes:
ArrayByteBufferPool, ArrayByteBufferPool.Quadratic, ArrayByteBufferPool.Tracking, ByteBufferPool.NonPooling, ByteBufferPool.Sized, 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

  • Field Details

  • 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.
    • removeAndRelease

      @Deprecated default boolean removeAndRelease(RetainableByteBuffer buffer)
      Deprecated.
      This API is experimental and may be removed in future releases
      Release the buffer in a way that will remove it from any pool that it may be in. If the buffer is not in a pool, calling this method is equivalent to calling Retainable.release(). Calling this method satisfies any contract that requires a call to Retainable.release().
      Returns:
      true if a call to Retainable.release() would have returned true.
      See Also:
    • clear

      void clear()

      Removes all non-retained pooled instances from this pool.