java.lang.Object
org.glassfish.grizzly.memory.PooledMemoryManager
- All Implemented Interfaces:
MemoryManager<Buffer>,WrapperAware,MonitoringAware<MemoryProbe>
A
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.
- The base size of the buffer for the 1st pool, every next pool n will have buffer size equal to bufferSize(n-1) * 2^growthFactor
- The number of pools, responsible for allocation of buffers of a pool-specific size
- The buffer size growth factor, that defines 2^x multiplier, used to calculate buffer size for next allocated pool
- The number of pool slices that every pool will stripe allocation requests across
- The percentage of the heap that this manager will use when populating the pools
- The percentage of buffers to be pre-allocated during MemoryManager initialization
- The flag indicating whether direct or heap based
Buffers will be allocated
- Base buffer size: 4 KiB (
DEFAULT_BASE_BUFFER_SIZE) - Number of pools: 3 (
DEFAULT_NUMBER_OF_POOLS) - Growth factor: 2 (
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 64KiB - Number of pool slices: Based on the return value of
Runtime.getRuntime().availableProcessors() - Percentage of heap: 3% (
DEFAULT_HEAP_USAGE_PERCENTAGE) - Percentage of buffers to be pre-allocated: 100% (
DEFAULT_PREALLOCATED_BUFFERS_PERCENTAGE) - Heap based
Buffers will be allocated
HeapMemoryManager 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.- Since:
- 2.3.11
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final intstatic final floatstatic final intstatic final floatprotected final DefaultMonitoringConfig<MemoryProbe>Basic monitoring support.Fields inherited from interface org.glassfish.grizzly.memory.MemoryManager
DEFAULT_MEMORY_MANAGER -
Constructor Summary
ConstructorsConstructorDescriptionCreates a newPooledMemoryManagerusing 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 onRuntime.getRuntime().availableProcessors()The initial allocation will use 3% of the heap The percentage of buffers to be pre-allocated during MemoryManager initializationPooledMemoryManager(boolean isDirect) Creates a newPooledMemoryManagerusing the specified parameters for configuration.PooledMemoryManager(int baseBufferSize, int numberOfPools, int growthFactor, int numberOfPoolSlices, float percentOfHeap, float percentPreallocated, boolean isDirect) Creates a newPooledMemoryManagerusing the specified parameters for configuration. -
Method Summary
Modifier and TypeMethodDescriptionallocate(int size) For this implementation, this method simply calls through toallocateAtLeast(int);allocateAtLeast(int size) Allocates a buffer of at least the size requested.protected ObjectReturn the object associatedMonitoringConfig.reallocate(Buffer oldBuffer, int newSize) Reallocates an existing buffer to at least the specified size.voidReleaseBuffer.booleanwillAllocateDirect(int size) Return true if nextMemoryManager.allocate(int)orMemoryManager.allocateAtLeast(int)call, made in the current thread for the given memory size, going to return aBufferbased on directByteBuffer, or false otherwise.wrap(byte[] data) ReturnsBuffer, which wraps the byte array.wrap(byte[] data, int offset, int length) ReturnsBuffer, which wraps the part of byte array with specific offset and length.wrap(ByteBuffer byteBuffer) ReturnsBuffer, which wraps theByteBuffer.
-
Field Details
-
DEFAULT_BASE_BUFFER_SIZE
public static final int DEFAULT_BASE_BUFFER_SIZE- See Also:
-
DEFAULT_NUMBER_OF_POOLS
public static final int DEFAULT_NUMBER_OF_POOLS- See Also:
-
DEFAULT_GROWTH_FACTOR
public static final int DEFAULT_GROWTH_FACTOR- See Also:
-
DEFAULT_HEAP_USAGE_PERCENTAGE
public static final float DEFAULT_HEAP_USAGE_PERCENTAGE- See Also:
-
DEFAULT_PREALLOCATED_BUFFERS_PERCENTAGE
public static final float DEFAULT_PREALLOCATED_BUFFERS_PERCENTAGE- See Also:
-
monitoringConfig
Basic monitoring support. Concrete implementations of this class need only to implement thecreateJmxManagementObject()method to plug into the Grizzly 2.0 JMX framework.
-
-
Constructor Details
-
PooledMemoryManager
public PooledMemoryManager()Creates a newPooledMemoryManagerusing 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 3% of the heap
- The percentage of buffers to be pre-allocated during MemoryManager initialization
-
PooledMemoryManager
public PooledMemoryManager(boolean isDirect) Creates a newPooledMemoryManagerusing the specified parameters for configuration.- Parameters:
isDirect- flag, indicating whether direct or heap basedBuffers will be allocated
-
PooledMemoryManager
public PooledMemoryManager(int baseBufferSize, int numberOfPools, int growthFactor, int numberOfPoolSlices, float percentOfHeap, float percentPreallocated, boolean isDirect) Creates a newPooledMemoryManagerusing the specified parameters for configuration.- Parameters:
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 pre-allocated during MemoryManager initializationisDirect- flag, indicating whether direct or heap basedBuffers will be allocated
-
-
Method Details
-
allocate
For this implementation, this method simply calls through toallocateAtLeast(int);- Specified by:
allocatein interfaceMemoryManager<Buffer>- Parameters:
size-Buffersize to be allocated.- Returns:
- allocated
Buffer.
-
allocateAtLeast
Allocates a buffer of at least the size requested. Keep in mind that the capacity of the buffer may be greater than the allocation request. The limit however, will be set to the specified size. The memory beyond the limit, is available for use.- Specified by:
allocateAtLeastin interfaceMemoryManager<Buffer>- Parameters:
size- the minBuffersize to be allocated.- Returns:
- a buffer with a limit of the specified size.
-
reallocate
Reallocates an existing buffer to at least the specified size.- Specified by:
reallocatein interfaceMemoryManager<Buffer>- Parameters:
oldBuffer- oldBufferto be reallocated.newSize- newBufferrequired size.- Returns:
- potentially a new buffer of at least the specified size.
-
release
ReleaseBuffer. Implementation may ignore releasing and let JVM Garbage collector to take care about theBuffer, or returnBufferto pool, in case of more complex MemoryManager implementation.- Specified by:
releasein interfaceMemoryManager<Buffer>- Parameters:
buffer-Bufferto be released.
-
willAllocateDirect
public boolean willAllocateDirect(int size) Return true if nextMemoryManager.allocate(int)orMemoryManager.allocateAtLeast(int)call, made in the current thread for the given memory size, going to return aBufferbased on directByteBuffer, or false otherwise.- Specified by:
willAllocateDirectin interfaceMemoryManager<Buffer>- Returns:
-
getMonitoringConfig
Return the object associatedMonitoringConfig.- Specified by:
getMonitoringConfigin interfaceMonitoringAware<MemoryProbe>- Returns:
- the object associated
MonitoringConfig.
-
wrap
Description copied from interface:WrapperAwareReturnsBuffer, which wraps the byte array.- Specified by:
wrapin interfaceWrapperAware- Parameters:
data- byte array to wrap- Returns:
Bufferwrapper on top of passed byte array.
-
wrap
Description copied from interface:WrapperAwareReturnsBuffer, which wraps the part of byte array with specific offset and length.- Specified by:
wrapin interfaceWrapperAware- Parameters:
data- byte array to wrapoffset- byte buffer offsetlength- byte buffer length- Returns:
Bufferwrapper on top of passed byte array.
-
wrap
Description copied from interface:WrapperAware- Specified by:
wrapin interfaceWrapperAware- Parameters:
s-String- Returns:
Bufferwrapper on top of passedString.
-
wrap
Description copied from interface:WrapperAware -
wrap
Description copied from interface:WrapperAwareReturnsBuffer, which wraps theByteBuffer.- Specified by:
wrapin interfaceWrapperAware- Parameters:
byteBuffer-ByteBufferto wrap- Returns:
Bufferwrapper on top of passedByteBuffer.
-
createJmxManagementObject
-