Class DirectBufferPool

java.lang.Object
org.apache.hadoop.util.DirectBufferPool

@LimitedPrivate({"HDFS","MapReduce"}) @Evolving public class DirectBufferPool extends Object
A simple class for pooling direct ByteBuffers. This is necessary because Direct Byte Buffers do not take up much space on the heap, and hence will not trigger GCs on their own. However, they do take native memory, and thus can cause high memory usage if not pooled. The pooled instances are referred to only via weak references, allowing them to be collected when a GC does run. This class only does effective pooling when many buffers will be allocated at the same size. There is no attempt to reuse larger buffers to satisfy smaller allocations.
  • Constructor Details

    • DirectBufferPool

      public DirectBufferPool()
  • Method Details

    • getBuffer

      public ByteBuffer getBuffer(int size)
      Allocate a direct buffer of the specified size, in bytes. If a pooled buffer is available, returns that. Otherwise allocates a new one.
      Parameters:
      size - size.
      Returns:
      ByteBuffer.
    • returnBuffer

      public void returnBuffer(ByteBuffer buf)
      Return a buffer into the pool. After being returned, the buffer may be recycled, so the user must not continue to use it in any way.
      Parameters:
      buf - the buffer to return