Class WeakReferencedElasticByteBufferPool

java.lang.Object
org.apache.hadoop.io.ElasticByteBufferPool
org.apache.hadoop.io.WeakReferencedElasticByteBufferPool
All Implemented Interfaces:
ByteBufferPool

@Private @Unstable public final class WeakReferencedElasticByteBufferPool extends ElasticByteBufferPool
Buffer pool implementation which uses weak references to store buffers in the pool, such that they are garbage collected when there are no references to the buffer during a gc run. This is important as direct buffers don't get garbage collected automatically during a gc run as they are not stored on heap memory. Also the buffers are stored in a tree map which helps in returning smallest buffer whose size is just greater than requested length. This is a thread safe implementation.
  • Constructor Details

    • WeakReferencedElasticByteBufferPool

      public WeakReferencedElasticByteBufferPool()
  • Method Details

    • getBuffer

      public ByteBuffer getBuffer(boolean direct, int length)
      Get a new direct ByteBuffer. The pool can provide this from removing a buffer from its internal cache, or by allocating a new buffer.
      Specified by:
      getBuffer in interface ByteBufferPool
      Overrides:
      getBuffer in class ElasticByteBufferPool
      Parameters:
      direct - whether we want a direct byte buffer or a heap one.
      length - length of requested buffer.
      Returns:
      returns equal or next greater than capacity buffer from pool if already available and not garbage collected else creates a new buffer and return it.
    • putBuffer

      public void putBuffer(ByteBuffer buffer)
      Return buffer to the pool.
      Specified by:
      putBuffer in interface ByteBufferPool
      Overrides:
      putBuffer in class ElasticByteBufferPool
      Parameters:
      buffer - buffer to be returned.
    • release

      public void release()
      Clear the buffer pool thus releasing all the buffers. The caller must remove all references of existing buffers before calling this method to avoid memory leaks.
    • getCurrentBuffersCount

      @VisibleForTesting public int getCurrentBuffersCount(boolean isDirect)
      Get current buffers count in the pool.
      Parameters:
      isDirect - whether we want to count the heap or direct buffers.
      Returns:
      count of buffers.