- java.lang.Object
-
- io.netty5.buffer.api.pool.PooledBufferAllocator
-
- All Implemented Interfaces:
BufferAllocator
,BufferAllocatorMetricProvider
,SafeCloseable
,AutoCloseable
public class PooledBufferAllocator extends Object implements BufferAllocator, BufferAllocatorMetricProvider
-
-
Constructor Summary
Constructors Constructor Description PooledBufferAllocator(MemoryManager manager, boolean direct)
PooledBufferAllocator(MemoryManager manager, boolean direct, int numArenas, int pageSize, int maxOrder)
PooledBufferAllocator(MemoryManager manager, boolean direct, int numArenas, int pageSize, int maxOrder, int smallCacheSize, int normalCacheSize, boolean useCacheForAllThreads)
PooledBufferAllocator(MemoryManager manager, boolean direct, int numArenas, int pageSize, int maxOrder, int smallCacheSize, int normalCacheSize, boolean useCacheForAllThreads, int directMemoryCacheAlignment)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Buffer
allocate(int size)
Allocate aBuffer
of the given size in bytes.void
close()
Close this allocator, freeing all of its internal resources.Supplier<Buffer>
constBufferSupplier(byte[] bytes)
Create a supplier of "constant" Buffers from this allocator, that all have the given byte contents.static int
defaultMaxOrder()
Default maximum order - System Property: io.netty5.allocator.maxOrder - default 11static int
defaultNormalCacheSize()
Default normal cache size - System Property: io.netty5.allocator.normalCacheSize - default 64static int
defaultNumDirectArena()
Default number of direct arenas - System Property: io.netty5.allocator.numDirectArenas - default 2 * coresstatic int
defaultNumHeapArena()
Default number of heap arenas - System Property: io.netty5.allocator.numHeapArenas - default 2 * coresstatic int
defaultPageSize()
Default buffer page size - System Property: io.netty5.allocator.pageSize - default 8192static boolean
defaultPreferDirect()
Default prefer direct - System Property: io.netty5.noPreferDirect - default falsestatic int
defaultSmallCacheSize()
Default small cache size - System Property: io.netty5.allocator.smallCacheSize - default 256static boolean
defaultUseCacheForAllThreads()
Default thread caching behavior - System Property: io.netty5.allocator.useCacheForAllThreads - default trueString
dumpStats()
Returns the status of the allocator (which contains all metrics) as string.AllocationType
getAllocationType()
Get theAllocationType
from this allocator.boolean
isDirectBufferPooled()
static boolean
isDirectMemoryCacheAlignmentSupported()
Returntrue
if direct memory cache alignment is supported,false
otherwise.boolean
isPooling()
Determine if this allocator is pooling and reusing its allocated memory.BufferAllocatorMetric
metric()
Returns aBufferAllocatorMetric
for aBufferAllocator
.int
numArenas()
boolean
trimCurrentThreadCache()
Trim thread local cache for the currentThread
, which will give back any cached memory that was not allocated frequently since the last trim operation.
-
-
-
Constructor Detail
-
PooledBufferAllocator
public PooledBufferAllocator(MemoryManager manager, boolean direct)
-
PooledBufferAllocator
public PooledBufferAllocator(MemoryManager manager, boolean direct, int numArenas, int pageSize, int maxOrder)
-
PooledBufferAllocator
public PooledBufferAllocator(MemoryManager manager, boolean direct, int numArenas, int pageSize, int maxOrder, int smallCacheSize, int normalCacheSize, boolean useCacheForAllThreads)
-
PooledBufferAllocator
public PooledBufferAllocator(MemoryManager manager, boolean direct, int numArenas, int pageSize, int maxOrder, int smallCacheSize, int normalCacheSize, boolean useCacheForAllThreads, int directMemoryCacheAlignment)
-
-
Method Detail
-
isPooling
public boolean isPooling()
Description copied from interface:BufferAllocator
Determine if this allocator is pooling and reusing its allocated memory.- Specified by:
isPooling
in interfaceBufferAllocator
- Returns:
true
if this allocator is pooling and reusing its memory,false
otherwise.
-
getAllocationType
public AllocationType getAllocationType()
Description copied from interface:BufferAllocator
Get theAllocationType
from this allocator. This would typically be one of theStandardAllocationTypes
.- Specified by:
getAllocationType
in interfaceBufferAllocator
- Returns:
- The type of allocations performed by this allocator.
-
allocate
public Buffer allocate(int size)
Description copied from interface:BufferAllocator
Allocate aBuffer
of the given size in bytes. This method may throw anOutOfMemoryError
if there is not enough free memory available to allocate aBuffer
of the requested size.The buffer will use big endian byte order.
- Specified by:
allocate
in interfaceBufferAllocator
- Parameters:
size
- The size ofBuffer
to allocate.- Returns:
- The newly allocated
Buffer
.
-
constBufferSupplier
public Supplier<Buffer> constBufferSupplier(byte[] bytes)
Description copied from interface:BufferAllocator
Create a supplier of "constant" Buffers from this allocator, that all have the given byte contents. The buffer has the same capacity as the byte array length, and its write offset is placed at the end, and its read offset is at the beginning, such that the entire buffer contents are readable.The buffers produced by the supplier will each have their own independent life-cycle, and closing them will make them inaccessible, just like normally allocated buffers.
The buffers produced are "constants", in the sense that they are read-only.
It can generally be expected, but is not guaranteed, that the returned supplier is more resource efficient than allocating and copying memory with other available APIs. In such optimised implementations, the underlying memory baking the buffers will be shared among all the buffers produced by the supplier.
The primary use case for this API, is when you need to repeatedly produce buffers with the same contents, and you perhaps wish to keep a
static final
field with these contents. The supplier-based API enforces that each usage get their own distinct buffer instance. Each of these instances cannot interfere with each other, so bugs like closing, or modifying the contents, of a shared buffer cannot occur.- Specified by:
constBufferSupplier
in interfaceBufferAllocator
- Parameters:
bytes
- The byte contents of the buffers produced by the returned supplier.- Returns:
- A supplier of read-only buffers with the given contents.
-
close
public void close()
Description copied from interface:BufferAllocator
Close this allocator, freeing all of its internal resources.Existing (currently in-use) allocated buffers will not be impacted by calling this method. If this is a pooling or caching allocator, then existing buffers will be immediately freed when they are closed, instead of being pooled or cached.
The allocator can no longer be used to allocate more buffers after calling this method. Attempting to allocate from a closed allocator will cause
IllegalStateException
s to be thrown.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceBufferAllocator
- Specified by:
close
in interfaceSafeCloseable
-
defaultNumHeapArena
public static int defaultNumHeapArena()
Default number of heap arenas - System Property: io.netty5.allocator.numHeapArenas - default 2 * cores
-
defaultNumDirectArena
public static int defaultNumDirectArena()
Default number of direct arenas - System Property: io.netty5.allocator.numDirectArenas - default 2 * cores
-
defaultPageSize
public static int defaultPageSize()
Default buffer page size - System Property: io.netty5.allocator.pageSize - default 8192
-
defaultMaxOrder
public static int defaultMaxOrder()
Default maximum order - System Property: io.netty5.allocator.maxOrder - default 11
-
defaultUseCacheForAllThreads
public static boolean defaultUseCacheForAllThreads()
Default thread caching behavior - System Property: io.netty5.allocator.useCacheForAllThreads - default true
-
defaultPreferDirect
public static boolean defaultPreferDirect()
Default prefer direct - System Property: io.netty5.noPreferDirect - default false
-
defaultSmallCacheSize
public static int defaultSmallCacheSize()
Default small cache size - System Property: io.netty5.allocator.smallCacheSize - default 256
-
defaultNormalCacheSize
public static int defaultNormalCacheSize()
Default normal cache size - System Property: io.netty5.allocator.normalCacheSize - default 64
-
isDirectMemoryCacheAlignmentSupported
public static boolean isDirectMemoryCacheAlignmentSupported()
Returntrue
if direct memory cache alignment is supported,false
otherwise.
-
isDirectBufferPooled
public boolean isDirectBufferPooled()
-
numArenas
public int numArenas()
-
metric
public BufferAllocatorMetric metric()
Description copied from interface:BufferAllocatorMetricProvider
Returns aBufferAllocatorMetric
for aBufferAllocator
.- Specified by:
metric
in interfaceBufferAllocatorMetricProvider
-
trimCurrentThreadCache
public boolean trimCurrentThreadCache()
-
dumpStats
public String dumpStats()
Returns the status of the allocator (which contains all metrics) as string. Be aware this may be expensive and so should not be called too frequently.
-
-