public class DirectBufferPool extends Object
ByteBuffer
s. Methods are provided to acquire a ByteBuffer
from the pool and to release a ByteBuffer
back to the pool.
Note: There is a bug in the release of large temporary direct
ByteBuffer
s which motivates this class. For regular journals that
overflow, the write cache is allocated once and handed off from journal to
journal. However, there is no such opportunity for temporary stores.
Unfortunately it is NOT an option to simply disable the write cache for
temporary stores since NIO will allocate (and fail to release) an "temporary"
direct buffer for the operation which transfers the data from the
TransientBufferStrategy
to disk. Therefore the data is copied into a
temporary buffer allocated from this pool and then the buffer is either
handed off to the IBufferStrategy
for use as its write cache (in
which case the TemporaryRawStore
holds a reference to the buffer and
releases it back to those pool when it is finalized) or the buffer is
immediately released back to this pool.
Note: Precisely because of the bug which motivates this class, we DO NOT
release buffers back to the JVM. This means that the size of a
DirectBufferPool
can only increase, but at least you get to (re-)use
the memory that you have allocated rather than leaking it to the native heap.
ab76d1d4479fffffffffa5abfb09c719a30?bug_id=6210541
Modifier and Type | Class and Description |
---|---|
static interface |
DirectBufferPool.Options
Options for provisioning the static instance of the
DirectBufferPool . |
Modifier and Type | Field and Description |
---|---|
static DirectBufferPool |
INSTANCE
A JVM-wide pool of direct
ByteBuffer s used for a variety of
purposes with a default DirectBufferPool.Options.BUFFER_CAPACITY of
1 MB . |
Modifier | Constructor and Description |
---|---|
protected |
DirectBufferPool(String name,
int poolCapacity,
int bufferCapacity)
Create a direct
ByteBuffer pool. |
Modifier and Type | Method and Description |
---|---|
IBufferAccess |
acquire()
Return an
IBufferAccess wrapping a direct ByteBuffer . |
IBufferAccess |
acquire(long timeout,
TimeUnit unit)
Return an
IBufferAccess wrapping a direct ByteBuffer . |
int |
getAcquiredBufferCount()
The #of
ByteBuffer s which are currently acquired. |
int |
getBufferCapacity()
The capacity in bytes of the
ByteBuffer s managed by this pool as
specified to the constructor. |
static CounterSet |
getCounters()
Return the
CounterSet for the DirectBufferPool . |
String |
getName()
The name of this buffer pool instance.
|
int |
getPoolCapacity()
The maximum capacity in buffers of the
DirectBufferPool as
specified to the constructor. |
int |
getPoolSize()
The approximate #of
ByteBuffer s currently managed by this
pool. |
public static final DirectBufferPool INSTANCE
ByteBuffer
s used for a variety of
purposes with a default DirectBufferPool.Options.BUFFER_CAPACITY
of
1 MB
.
Note: acquire()
requests will block once the pool capacity has
been reached until a buffer is release(ByteBuffer)
ed.
protected DirectBufferPool(String name, int poolCapacity, int bufferCapacity)
ByteBuffer
pool.
Note: When the poolSize is bounded then acquire()
MAY
block. This can introduce deadlocks into the application. You can use a
timeout to limit the sensitivity to deadlocks or you can use an unbounded
pool and accept that OutOfMemoryError
s will arise if there is
too much concurrent demand for the buffers supplied by this pool.
poolCapacity
- The maximum capacity of the pool. Use
Integer.MAX_VALUE
to have a pool with an unbounded
number of buffers.bufferCapacity
- The capacity of the ByteBuffer
s managed by this pool.INSTANCE
public String getName()
public int getAcquiredBufferCount()
ByteBuffer
s which are currently acquired. This counter is
incremented when a buffer is acquired and decremented when a buffer is
released.public int getPoolCapacity()
DirectBufferPool
as
specified to the constructor.public int getPoolSize()
ByteBuffer
s currently managed by this
pool.public int getBufferCapacity()
ByteBuffer
s managed by this pool as
specified to the constructor.public IBufferAccess acquire() throws InterruptedException
IBufferAccess
wrapping a direct ByteBuffer
. The
capacity of the buffer is determined by the configuration of this pool.
The position will be equal to zero, the limit will be equal to the
capacity, and the mark will not be set.
Note: This method will block if there are no free buffers in the pool and
the pool was configured with a maximum capacity. It WILL log an error if
it blocks. While blocking is not very safe, using a heap (vs direct)
ByteBuffer
is not very safe either since Java NIO will allocate a
temporary direct ByteBuffer
for IOs and that can both run out of
memory and leak memory.
ByteBuffer
.InterruptedException
- if the caller's Thread
is interrupted awaiting a
buffer.OutOfMemoryError
- if there is not enough free memory to fulfill the request.public IBufferAccess acquire(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException
IBufferAccess
wrapping a direct ByteBuffer
. The
capacity of the buffer is determined by the configuration of this pool.
The position will be equal to zero, the limit will be equal to the
capacity, and the mark will not be set.
Note: This method will block if there are no free buffers in the pool and
the pool was configured with a maximum capacity. In addition it MAY block
if there is not enough free memory to fulfill the request. It WILL log an
error if it blocks. While blocking is not very safe, using a heap (vs
direct) ByteBuffer
is not very safe either since Java NIO will
allocate a temporary direct ByteBuffer
for IOs and that can both
run out of memory and leak memory.
timeout
- The timeout.unit
- The units for that timeout.ByteBuffer
.InterruptedException
- if the caller's Thread
is interrupted awaiting a
buffer.TimeoutException
public static CounterSet getCounters()
CounterSet
for the DirectBufferPool
.
ByteBuffer
s currently
managed by the pool.ByteBuffer
s that may be
allocated by the pool.ByteBuffer
associated
with a given pool.ByteBuffer
s currently acquired by the
application.Copyright © 2006–2019 SYSTAP, LLC DBA Blazegraph. All rights reserved.