Package org.apache.hadoop.io
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.
-
Nested Class Summary
Nested classes/interfaces inherited from class org.apache.hadoop.io.ElasticByteBufferPool
ElasticByteBufferPool.Key -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongetBuffer(boolean direct, int length) Get a new direct ByteBuffer.intgetCurrentBuffersCount(boolean isDirect) Get current buffers count in the pool.voidputBuffer(ByteBuffer buffer) Return buffer to the pool.voidrelease()Clear the buffer pool thus releasing all the buffers.Methods inherited from class org.apache.hadoop.io.ElasticByteBufferPool
size
-
Constructor Details
-
WeakReferencedElasticByteBufferPool
public WeakReferencedElasticByteBufferPool()
-
-
Method Details
-
getBuffer
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:
getBufferin interfaceByteBufferPool- Overrides:
getBufferin classElasticByteBufferPool- 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
Return buffer to the pool.- Specified by:
putBufferin interfaceByteBufferPool- Overrides:
putBufferin classElasticByteBufferPool- 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.
-