public class CachingAllocator extends DefaultAllocator
ByteBuffer allocations.
It uses N number of caches for storing released buffers. The caches are organized to store buffers of different capacities. The first N-1 caches are assigned to store buffers of fixed capacities according to the following rules:
cache 1: stores buffers of capacity determined by the minCapacity cache 2: stores buffers of capacity determined by the minCapacity * 2 cache 3: stores buffers of capacity determined by the minCapacity * 4 ... cache N-1: stores buffers of capacity determined by the minCapacity * 2 to the power (N-2)The last N-th cache is assign to store buffers of capacities greater or equal to
minCapacity * 2 to the power (N-1). Its current capacity adjusts
to the capacities of released buffers. If the capacity of released buffer is
greater than the current capacity then all currently stored buffers are
garbage collected and the current capacity adjusts to the capacity of the
buffer being just released.
It implements cache aging mechanism to reduce size of caches that are not used for longer time.
Performance and Scalability:
It uses one set of caches so it can significantly reduce memory
consumption but on the other hand, due to a need for cache synchronization,
it can introduce latency issues in multi-thread applications.
The behavior of the allocator can be customized by setting following system properties:
DEFAULT, direct| Constructor and Description |
|---|
CachingAllocator(boolean direct)
Constructs a caching allocator with default minimal capacity (128).
|
CachingAllocator(boolean direct,
IDefaultAllocatorMetricCollector metric)
Constructs a caching allocator with default minimal capacity (128) and specified
metric data collector.
|
CachingAllocator(boolean direct,
int minCapacity)
Constructs a caching allocator.
|
CachingAllocator(boolean direct,
int minCapacity,
IDefaultAllocatorMetricCollector metric)
Constructs a caching allocator with specified metric data collector.
|
| Modifier and Type | Method and Description |
|---|---|
protected ByteBuffer |
allocate(int capacity,
boolean direct)
Allocates a new buffer with given capacity and type
|
protected ByteBuffer |
allocate(int capacity,
ByteBuffer buffer)
Allocates a new buffer that will replace the original not empty buffer.
|
protected ByteBuffer |
allocateEmpty(int capacity,
ByteBuffer buffer)
Allocates a new buffer that will replace the original empty buffer.
|
int |
getMinCapacity()
Gets the minimal capacity for buffers allocated by this allocator.
|
boolean |
isReleasable()
Tells if an implementation of the allocator supports releasing of no
longer used buffers.
|
void |
purge()
Purges all caches used by this allocator.
|
void |
release(ByteBuffer buffer)
Informs that given buffer that was allocated by this allocator is no
longer used and can be released.
|
allocate, ensure, ensureSome, extend, reduce, usesArraypublic CachingAllocator(boolean direct,
IDefaultAllocatorMetricCollector metric)
direct - true if the allocator should allocate direct
buffers, or false to allocate non-direct buffers
that have a backing arraymetric - a metric data collectorpublic CachingAllocator(boolean direct)
direct - true if the allocator should allocate direct
buffers, or false to allocate non-direct buffers
that have a backing arraypublic CachingAllocator(boolean direct,
int minCapacity,
IDefaultAllocatorMetricCollector metric)
direct - true if the allocator should allocate direct
buffers, or false to allocate non-direct buffers
that have a backing arrayminCapacity - the minimal capacity for buffers allocated by this
allocatormetric - a metric data collectorpublic CachingAllocator(boolean direct,
int minCapacity)
direct - true if the allocator should allocate direct
buffers, or false to allocate non-direct buffers
that have a backing arrayminCapacity - the minimal capacity for buffers allocated by this
allocatorpublic final int getMinCapacity()
public void purge()
public boolean isReleasable()
DefaultAllocatorisReleasable in interface IByteBufferAllocatorisReleasable in class DefaultAllocatorfalsepublic void release(ByteBuffer buffer)
IByteBufferAllocatorrelease in interface IByteBufferAllocatorrelease in class DefaultAllocatorbuffer - buffer to be releasedprotected ByteBuffer allocate(int capacity, boolean direct)
DefaultAllocatorallocate in class DefaultAllocatorcapacity - the capacity of the allocated bufferdirect - the type of the allocated bufferprotected ByteBuffer allocateEmpty(int capacity, ByteBuffer buffer)
DefaultAllocatorallocateEmpty in class DefaultAllocatorcapacity - the capacity of the allocated bufferbuffer - the buffer to be replaced. If the allocator support releasing it
can be safely released in this method.protected ByteBuffer allocate(int capacity, ByteBuffer buffer)
DefaultAllocatorallocate in class DefaultAllocatorcapacity - the capacity of the allocated bufferbuffer - the buffer to be replaced. If the allocator support releasing it
can be safely released in this method.Copyright © 2017–2022 SNF4J.ORG. All rights reserved.