public class PooledMemoryManager extends Object implements MemoryManager<Buffer>, WrapperAware
MemoryManager
implementation based on a series of shared memory pools.
Each pool contains multiple buffers of the fixed length specific for this pool.
There are several tuning options for this MemoryManager
implementation.
Buffer
s will be allocatedDEFAULT_BASE_BUFFER_SIZE
)DEFAULT_NUMBER_OF_POOLS
)DEFAULT_GROWTH_FACTOR
), which means the first buffer pool will contains buffer of size 4 KiB, the seconds one buffer of size 16KiB, the third one buffer of size 64KiBRuntime.getRuntime().availableProcessors()
DEFAULT_HEAP_USAGE_PERCENTAGE
)DEFAULT_PREALLOCATED_BUFFERS_PERCENTAGE
)Buffer
s will be allocatedHeapMemoryManager
or
ByteBufferManager
is that this implementation doesn't use ThreadLocal pools
and as such, doesn't suffer from the memory fragmentation/reallocation cycle that can impact the ThreadLocal versions.Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_BASE_BUFFER_SIZE |
static int |
DEFAULT_GROWTH_FACTOR |
static float |
DEFAULT_HEAP_USAGE_PERCENTAGE |
static int |
DEFAULT_NUMBER_OF_POOLS |
static float |
DEFAULT_PREALLOCATED_BUFFERS_PERCENTAGE |
protected DefaultMonitoringConfig<MemoryProbe> |
monitoringConfig
Basic monitoring support.
|
DEFAULT_MEMORY_MANAGER
Constructor and Description |
---|
PooledMemoryManager()
Creates a new
PooledMemoryManager using the following defaults:
4 KiB base buffer size
3 pools
2 growth factor, which means 1st pool will contain buffers of size 4KiB, the 2nd - 16KiB, the 3rd - 64KiB
Number of pool slices based on Runtime.getRuntime().availableProcessors()
The initial allocation will use 10% of the heap
The percentage of buffers to be preallocated during MemoryManager initialization
|
PooledMemoryManager(boolean isDirect)
Creates a new
PooledMemoryManager using the specified parameters for configuration. |
PooledMemoryManager(int baseBufferSize,
int numberOfPools,
int growthFactor,
int numberOfPoolSlices,
float percentOfHeap,
float percentPreallocated,
boolean isDirect)
Creates a new
PooledMemoryManager using the specified parameters for configuration. |
Modifier and Type | Method and Description |
---|---|
Buffer |
allocate(int size)
For this implementation, this method simply calls through to
allocateAtLeast(int) ; |
Buffer |
allocateAtLeast(int size)
Allocates a buffer of at least the size requested.
|
protected Object |
createJmxManagementObject() |
MonitoringConfig<MemoryProbe> |
getMonitoringConfig()
Return the object associated
MonitoringConfig . |
Buffer |
reallocate(Buffer oldBuffer,
int newSize)
Reallocates an existing buffer to at least the specified size.
|
void |
release(Buffer buffer)
Release
Buffer . |
boolean |
willAllocateDirect(int size)
Return true if next
MemoryManager.allocate(int) or MemoryManager.allocateAtLeast(int) call,
made in the current thread for the given memory size, going to return a Buffer based
on direct ByteBuffer , or false otherwise. |
Buffer |
wrap(byte[] data)
Returns
Buffer , which wraps the byte array. |
Buffer |
wrap(byte[] data,
int offset,
int length)
Returns
Buffer , which wraps the part of byte array with
specific offset and length. |
Buffer |
wrap(ByteBuffer byteBuffer)
Returns
Buffer , which wraps the ByteBuffer . |
Buffer |
wrap(String s)
|
Buffer |
wrap(String s,
Charset charset)
|
public static final int DEFAULT_BASE_BUFFER_SIZE
public static final int DEFAULT_NUMBER_OF_POOLS
public static final int DEFAULT_GROWTH_FACTOR
public static final float DEFAULT_HEAP_USAGE_PERCENTAGE
public static final float DEFAULT_PREALLOCATED_BUFFERS_PERCENTAGE
protected final DefaultMonitoringConfig<MemoryProbe> monitoringConfig
createJmxManagementObject()
method
to plug into the Grizzly 2.0 JMX framework.public PooledMemoryManager()
PooledMemoryManager
using the following defaults:
Runtime.getRuntime().availableProcessors()
public PooledMemoryManager(boolean isDirect)
PooledMemoryManager
using the specified parameters for configuration.isDirect
- flag, indicating whether direct or heap based Buffer
s will be allocatedpublic PooledMemoryManager(int baseBufferSize, int numberOfPools, int growthFactor, int numberOfPoolSlices, float percentOfHeap, float percentPreallocated, boolean isDirect)
PooledMemoryManager
using the specified parameters for configuration.baseBufferSize
- the base size of the buffer for the 1st pool, every next pool n will have buffer size equal to bufferSize(n-1) * 2^growthFactornumberOfPools
- the number of pools, responsible for allocation of buffers of a pool-specific sizegrowthFactor
- the buffer size growth factor, that defines 2^x multiplier, used to calculate buffer size for next allocated poolnumberOfPoolSlices
- the number of pool slices that every pool will stripe allocation requests acrosspercentOfHeap
- percentage of the heap that will be used when populating the poolspercentPreallocated
- percentage of buffers to be preallocated during MemoryManager initializationisDirect
- flag, indicating whether direct or heap based Buffer
s will be allocatedpublic Buffer allocate(int size)
allocateAtLeast(int)
;allocate
in interface MemoryManager<Buffer>
size
- Buffer
size to be allocated.Buffer
.public Buffer allocateAtLeast(int size)
allocateAtLeast
in interface MemoryManager<Buffer>
size
- the min Buffer
size to be allocated.public Buffer reallocate(Buffer oldBuffer, int newSize)
reallocate
in interface MemoryManager<Buffer>
oldBuffer
- old Buffer
to be reallocated.newSize
- new Buffer
required size.public void release(Buffer buffer)
Buffer
.
Implementation may ignore releasing and let JVM Garbage collector to take
care about the Buffer
, or return Buffer
to pool, in case
of more complex MemoryManager implementation.release
in interface MemoryManager<Buffer>
buffer
- Buffer
to be released.public boolean willAllocateDirect(int size)
MemoryManager.allocate(int)
or MemoryManager.allocateAtLeast(int)
call,
made in the current thread for the given memory size, going to return a Buffer
based
on direct ByteBuffer
, or false otherwise.willAllocateDirect
in interface MemoryManager<Buffer>
public MonitoringConfig<MemoryProbe> getMonitoringConfig()
MonitoringConfig
.getMonitoringConfig
in interface MonitoringAware<MemoryProbe>
MonitoringConfig
.public Buffer wrap(byte[] data)
WrapperAware
Buffer
, which wraps the byte array.wrap
in interface WrapperAware
data
- byte array to wrapBuffer
wrapper on top of passed byte array.public Buffer wrap(byte[] data, int offset, int length)
WrapperAware
Buffer
, which wraps the part of byte array with
specific offset and length.wrap
in interface WrapperAware
data
- byte array to wrapoffset
- byte buffer offsetlength
- byte buffer lengthBuffer
wrapper on top of passed byte array.public Buffer wrap(String s)
WrapperAware
wrap
in interface WrapperAware
s
- String
Buffer
wrapper on top of passed String
.public Buffer wrap(String s, Charset charset)
WrapperAware
public Buffer wrap(ByteBuffer byteBuffer)
WrapperAware
Buffer
, which wraps the ByteBuffer
.wrap
in interface WrapperAware
byteBuffer
- ByteBuffer
to wrapBuffer
wrapper on top of passed ByteBuffer
.protected Object createJmxManagementObject()
Copyright © 2015 Oracle Corporation. All rights reserved.